块语句

块语句用于对零个或多个语句进行分组。该块由一对大括号 ("大括号") 分隔,并包含零个或多个语句和声明的列表。

¥A block statement is used to group zero or more statements. The block is delimited by a pair of braces ("curly braces") and contains a list of zero or more statements and declarations.

Try it

语法

¥Syntax

js
{
  StatementList
}
StatementList

语句和声明分组在块语句内。

描述

¥Description

块语句在其他语言中通常被称为复合语句。它允许你使用多个语句,而 JavaScript 只需要一个语句。将语句组合成块是 JavaScript 中的常见做法,尤其是与 if...elsefor 等控制流语句结合使用时。使用 空语句 可能会出现相反的行为,尽管需要提供声明,但你无需提供声明。

¥The block statement is often called the compound statement in other languages. It allows you to use multiple statements where JavaScript expects only one statement. Combining statements into blocks is a common practice in JavaScript, especially when used in association with control flow statements like if...else and for. The opposite behavior is possible using an empty statement, where you provide no statement, although one is required.

此外,与 letconstclass 等块范围声明相结合,块可以防止临时变量污染全局命名空间,就像 IIFEs 一样。

¥In addition, combined with block-scoped declarations like let, const, and class, blocks can prevent temporary variables from polluting the global namespace, just like IIFEs do.

非严格模式下使用 var 或函数声明的块作用域规则

¥Block scoping rules with var or function declaration in non-strict mode

在非严格模式下使用 var 声明或由 函数声明 创建的变量没有块作用域。块中引入的变量的范围仅限于包含的函数或脚本,并且设置它们的效果在块本身之外仍然存在。例如:

¥Variables declared with var or created by function declarations in non-strict mode do not have block scope. Variables introduced within a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. For example:

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

这会记录 2,因为该块内的 var x 语句与该块之前的 var x 语句位于同一范围内。

¥This logs 2 because the var x statement within the block is in the same scope as the var x statement before the block.

在非严格代码中,块内的函数声明行为很奇怪。不要使用它们。

¥In non-strict code, function declarations inside blocks behave strangely. Do not use them.

在严格模式下使用 let、const、类或函数声明来阻止作用域规则

¥Block scoping rules with let, const, class, or function declaration in strict mode

相比之下,用 letconstclass 声明的标识符确实具有块作用域:

¥By contrast, identifiers declared with let, const, and class do have block scope:

js
let x = 1;
{
  let x = 2;
}
console.log(x); // 1

x = 2 的范围仅限于定义它的块。

¥The x = 2 is limited in scope to the block in which it was defined.

const 也是如此:

¥The same is true of const:

js
const c = 1;
{
  const c = 2;
}
console.log(c); // 1; does not throw SyntaxError

请注意,块作用域的 const c = 2 不会抛出 SyntaxError: Identifier 'c' has already been declared,因为它可以在块内唯一声明。

¥Note that the block-scoped const c = 2 does not throw a SyntaxError: Identifier 'c' has already been declared because it can be declared uniquely within the block.

严格模式 中,块内的函数声明的作用域为该块,并提升到块的顶部。

¥In strict mode, function declarations inside blocks are scoped to that block and are hoisted to the top of the block.

js
"use strict";

{
  foo(); // Logs "foo"
  function foo() {
    console.log("foo");
  }
}

foo(); // ReferenceError: foo is not defined

示例

¥Examples

使用块语句作为 for 循环体

¥Using a block statement as the body of a for loop

for 循环接受单个语句作为其主体。

¥A for loop accepts a single statement as its body.

js
for (let i = 0; i < 10; i++) console.log(i);

如果要在循环体中使用多个语句,可以将它们分组为一个块语句:

¥If you want to use more than one statement in the loop body, you can group them into one block statement:

js
for (let i = 0; i < 10; i++) {
  console.log(i);
  console.log(i ** 2);
}

使用块语句封装数据

¥Using a block statement to encapsulate data

letconst 声明的范围仅限于包含块。这允许你隐藏全局范围内的数据,而无需将其封装在函数中。

¥let and const declarations are scoped to the containing block. This allows you to hide data from the global scope without wrapping it in a function.

js
let sector;
{
  // These variables are scoped to this block and are not
  // accessible after the block
  const angle = Math.PI / 3;
  const radius = 10;
  sector = {
    radius,
    angle,
    area: (angle / 2) * radius ** 2,
    perimeter: 2 * radius + angle * radius,
  };
}
console.log(sector);
// {
//   radius: 10,
//   angle: 1.0471975511965976,
//   area: 52.35987755982988,
//   perimeter: 30.471975511965976
// }
console.log(typeof radius); // "undefined"

规范

Specification
ECMAScript Language Specification
# sec-block

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also