Scope of Variables in JavaScript
Scope of Variables in JavaScript
A little piece of JavaScript trivia; the scope of JavaScript variables is decided at parse time, not at run time, just like named functions. An example:
function () { foo = 'bar'; var foo }
The scope of the variable foo is the function body, even though the variable is declared in the statement after an assigment to the variable. It works in my the same way that this runs without error:
function () { var bar = foo(); function foo () { return 'baz'; } }
Even though the function foo is not defined in the code until after it is called, the parser picks up the name of the function so that the run time knows what to do.

0 Comments:
Post a Comment
<< Home