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

Block statement

时间:2015-06-26 01:39:59      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

 

The most basic statement is a block statement that is used to group statements. The block is delimited by a pair of curly brackets:

{
  statement_1;
  statement_2;
  .
  .
  .
  statement_n;
}

  

Example

Block statements are commonly used with control flow statements (e.g. ifforwhile).

while (x < 10) {
  x++;
}
 

  

Here, { x++; } is the block statement.

Important: JavaScript does not have block scope prior to ECMAScript 6. Variables introduced within a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope. Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don‘t do what you think they do, if you think they do anything like such blocks in C or Java. For example:

var x = 1;
{
  var x = 2;
}
console.log(x); // outputs 2

  

 

This outputs 2 because the var x statement within the block is in the same scope as thevar x statement before the block. In C or Java, the equivalent code would have outputted 1.

Starting with ECMAScript 6, the let variable declaration is block scoped. See the letreference page for more information.

Block statement

标签:

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

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