zoukankan      html  css  js  c++  java
  • javascript block

    概览

    语句块 (或其他语言中的 复合语句) 用来组织零个或多条语句. 用一对花括号界定语句块.

    语法

    {
      statement_1;
      statement_2;
      ...
      statement_n;
    }
    

      

    statement_1statement_2statement_n
    语句都包裹在语句块中.

    说明

    语句块通常在流程控制语句 (如 ifforwhile)中使用.如:

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

      

     

    注意语句块不是以分号结尾.

    其他语言中通常将语句块称为 复合语句. 语句块允许你在Javascript需要一行语句的时候使用多行语句. 在JavaScript中使用语句块是种很常见的做法. 与之相反的做法是使用 empty语句, empty语句 可以在需要语句的环境下不提供任何语句.

    没有块级作用域

    重要提示: 通过var声明的变量没有块级作用域. Variables introduced with a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. 换句话说, 语句块与作用域无关. 尽管单独的语句块是合法的语句, 但在JavaScript中你不会想使用单独的语句块,因为它们不像你想象的C或Java中的语句块那样处理事物. 例如:

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

      

     

    该代码段会输出2,因为块中的 var x 语句与块前面的var x 语句作用域相同. 在 C 或 Java中, 这段代码会输出1.

    规范

    SpecificationStatusComment
    ECMAScript 1st Edition. Standard Initial definition. Implemented in JavaScript 1.0
    ECMAScript 5.1 (ECMA-262)
    Block statement
    Standard  
    ECMAScript 6 (ECMA-262)
    Block statement
    Release Candidate  

    浏览器兼容性

    FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
  • 相关阅读:
    Account group in ERP and its mapping relationship with CRM partner group
    错误消息Number not in interval XXX when downloading
    错误消息Form of address 0001 not designated for organization
    Algorithm类介绍(core)
    梯度下降与随机梯度下降
    反思
    绘图: matplotlib核心剖析
    ORB
    SIFT
    Harris角点
  • 原文地址:https://www.cnblogs.com/hephec/p/4589962.html
Copyright © 2011-2022 走看看