Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!