码迷,mamicode.com
首页 > 其他好文 > 详细

Variable scope

时间:2015-06-26 00:17:50      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

 

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

  

Variable scope

标签:

原文地址:http://www.cnblogs.com/hephec/p/4601137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!