Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
Lexical Environment and bind()

When creating a function with bind() is a lexical environment also created? My code works as expected, I am making a Partially applied function with an object and I want to confirm if the reference is never lost when we use currying

    let literal = {
    
        nombre: 'Daniel'
    };
    function parcial(unObjeto, numero) {
    
        console.log(unObjeto);
    }
    let vinculada = parcial.bind(null, literal);
    vinculada(); // {nombre: 'Daniel'}
    
    literal.nuevapropiedad = 'nuevo valor';
    vinculada(); // {nombre: 'Daniel', nuevapropiedad: 'nuevo valor'}

I see that if I modify the "literal" object later in the code the linked function reads that "updated" object.

To the best of my knowledge this has logic because bind() creates a lexical environment that closes in this case on "literal" as if it did with the first argument (in this case I don't want to use the first argument) and it will never lose the reference.

Basically I want to keep the reference to "literal" in my function so I don't have to worry about passing it back every time I change "literal".

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Lexical environment is about scope. And words used to describe scope include things like close over and global etc.

However, .bind() does not work with scope. Instead it works with bindings. Binding is a concept orthogonal to scope. Where scope defines what variables are accessible in what part of code, binding defines what on what objects do methods operate on.

They are not the same concept (though languages like Java confuse the issue because it does not actually have a real concept of multiple scopes but instead calls binding "scope").

This is scope:

var a = 1;
var b = 2;

function foo (c) {
    var d = 3;
    var b = 4;

    a = 5;

    console.log(a, b, c, d);
}

foo(10); // logs 5, 4, 10, 3

console.log(a, b); // logs 5, 2
console.log(c, d); // throws error because c and d don't exist

This is binding:

var x = {}

x.hello = 1;

x.sayHello = function () {
    console.log(this.hello);
}

x.sayHello(); // logs 1

Function arguments, global variables, variables inside functions etc. are handled by the scope mechanism which in the case of javascript creates closures. They don't have anything to do with binding.

The value of this is handled by the binding mechanism (inheritance, .bind() etc.) and don't have anything to do with scope.

Basically .bind() does not affect the lexical environment and the lexical environment does not affect bindings (though you could argue that arrow functions are a kind of lexical binding).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda