Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

271
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda