Reputation: 7752
I understand that when I define a function in some lexical environment, it decides the lexical closure the function has access to and also the variables from outer scopes and in the prototype chain.
My question is what happens when we change the execution context of a function explicitly using bind
or call
or apply
. How does this affect the variables that ought to have been available in the function due to lexical closure and also variables that were supposed to be found in the prototype chain.
My instinct says the previous closure is replaced and a new prototype chain should be available and variable might or might not be found in the new prototype chain based on scenario, but then it begs the question if I am creating a new execution context, thereby there is a new lexical closure then can I somehow insert some other variables into this newly created lexical closure?
Also is there a way to change the this
of a function before it is called but preserve its previous lexical closure?
Upvotes: 0
Views: 46
Reputation: 4778
bind
, call
and apply
only change the this
value of a function (i.e. its context), not its scope (i.e. environment).
So any parent env. variables are still available, even when using the mentioned methods. Only this
will be changed.
Upvotes: 1