Saturday, July 11, 2015

Lexical Scope and Closures

Lexical scope

Lexical scope means compile time scope. That means implicitly we can see what will be the scopes for each code. Most important thing to remember is, although javascript uses curly braces ({}), a new scope is created only when you create a new function.

Closure

Closure is when a function remembers it's lexical scope even when the function is executed outside that lexical scope.
In line 8, we pass "baz" function to "bam" function. Then "baz" function will execute outside of "foo" function. But still it has access to "bar" variable. That is a closure.

No comments:

Post a Comment