标签:
When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.
JavaScript before ECMAScript 6 does not have block statement scope; rather, a variable declared within a block is local to the function (or global scope) that the block resides within. For example the following code will log 5
, because the scope of x
is the function (or global context) within which x
is declared, not the block, which in this case is an if
statement.
if (true) { var x = 5; } console.log(x); // 5
This behavior changes, when using the let
declaration introduced in ECMAScript 6.
if (true) { let y = 5; } console.log(y); // ReferenceError: y is not defined
标签:
原文地址:http://www.cnblogs.com/hephec/p/4601137.html