Content deleted Content added
mNo edit summary |
|||
Line 84:
Variables are [[lexical scoping|lexically scoped]] at [[function scope|function level]] (not [[block scope|block level]] as in C), and this does not depend on order ([[forward declaration]] is not necessary): if a variable is declared inside a function (at any point, in any block), then inside the function, the name will resolve to that variable. This is equivalent in block scoping to variables being forward declared at the top of the function, and is referred to as ''{{visible anchor|hoisting}}''.<ref>"[http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html JavaScript Scoping and Hoisting]", [http://www.adequatelygood.com/about.html Ben Cherry], ''[http://www.adequatelygood.com/ Adequately Good],'' 2010-02-08</ref>
However, the variable value is <code>undefined</code> until it is initialized, and [[forward reference]] is not possible. Thus a {{code|lang=javascript|code=var x = 1}} statement in the middle of the function is equivalent to a
Function statements, whose effect is to declare a variable of type <code>Function</code> and assign a value to it, are similar to variable statements, but in addition to hoisting the declaration, they also hoist the assignment – as if the entire statement appeared at the top of the containing function – and thus forward reference is also possible: the ___location of a function statement within an enclosing function is irrelevant.
|