In the event of a function that takes a callback function as one of its parameters. What would be the lexical environment of this function? The global lexical environment or the function from which the function is defined?
Asked
Active
Viewed 107 times
0
-
Every function creates its own lexical environment when it is getting called. It can additionally access variables from the scope where the function itself was defined, all the way up to the global scope. – Bergi Oct 05 '20 at 21:58
1 Answers
0
In short, your callback function has access to the scope where it was defined (including any parent scope like global or window). It doesn't have access to variables defined in the function that ultimately invokes it.
For a good explanation, read: what is the lexical environment of a function argument?

dwosk
- 1,202
- 8
- 11
-
A callback function *is* a function argument, so the questions are the same. – Heretic Monkey Oct 05 '20 at 20:31