I was just curious, if we pass the function let's say to another function, then is the lexical environment of the function passed with it or it is referred from the original parent function every time.
Every JS function is a closure so it has its lexical environment "attached" internally.
You can think of it as a special, internal property [[Scope]] on a function that it uses whenever it needs to access external variables.
It's quite similar to object [[Prototype]] used to look up for properties whenever they can't be found on an object itself but unlike [[Scope]] you can access [[Prototype]] with __proto__ or Object.getPrototypeOf() so it can be easier to understand.
Both of them might look magical at first but they're just chains of connected structures looked up in order from closest to farthest in search for properties/variables.
I was just curious, if we pass the function let's say to another function, then is the lexical environment of the function passed with it or it is referred from the original parent function every time.
Every JS function is a closure so it has its lexical environment "attached" internally.
You can think of it as a special, internal property
[[Scope]]
on a function that it uses whenever it needs to access external variables.It's quite similar to object
[[Prototype]]
used to look up for properties whenever they can't be found on an object itself but unlike[[Scope]]
you can access[[Prototype]]
with__proto__
orObject.getPrototypeOf()
so it can be easier to understand.Both of them might look magical at first but they're just chains of connected structures looked up in order from closest to farthest in search for properties/variables.
oh, thanks!!
your comment always motivates me to dig much deeper into the topic
You're welcome.