3 ms·
No. Function scope is the entire point of closures. If it was lexical scope it would not be a closure, it would just be a regular function. Many more language
by cousin_it 18y ago
No.
Function scope is the entire point of closures. If it was lexical scope it would not be a closure, it would just be a regular function.
Many more languages with closures have lexical scope than function scope. Lexical scope is the entire point of closures in those languages, indeed everywhere except JavaScript.
Just pass in j when you call it and you can have lexical scope easily.
I don't know j when I call the closure. Give a code example.
But if you changed that, a TON of code would be impossible, so I'm very happy with it working that way.
I can't imagine any code that would become impossible, or even the slightest bit more difficult, if function scope were replaced with lexical. Indeed all code using function scope could be mechanically translated into a (hypothetical) lexically-scoped-JavaScript by moving every var declaration in every if/for/while/etc block up to the toplevel of the containing function. If you disagree, give a code example.