首页 > 其他 > 详细

Variable scope

时间:2015-06-26 00:16:00      阅读:198      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!