javascript ES6 中的这个匿名块是什么?
what is this anonymous block in javascript ES6?
我正在阅读 pragmatists 的 ES6 新特性。但正如您所看到的,他们在该函数中使用了 匿名块 。
有人可以解释一下那是什么意思。它是任何 javascript 对象还是什么?我们如何使用它?还请为此提供一些参考。
function f() {
var x = 1
let y = 2
const z = 3
{
var x = 100
let y = 200
const z = 300
console.log('x in block scope is', x)
console.log('y in block scope is', y)
console.log('z in block scope is', z)
}
console.log('x outside of block scope is', x)
console.log('y outside of block scope is', y)
console.log('z outside of block scope is', z)
}
f()
这只是一个光秃秃的街区。任何时候遇到裸块,你都可以认为它等同于:
for (let i = 0; i < 1; i++) {
// block contents here
}
或
if (true) {
// block contents here
}
没什么特别的,也很少用到。它最重要的部分可能是如何在其中声明的 const
和 let
变量的作用域 仅限于块 ,而不是外部函数。
它与 Javascript 中的所有非功能块具有相同的行为 - 就像 if
块、while
块或 for
块,只是没有条件进入,正好执行一次
匿名块在使用 let 变量时很有用。 var
关键字声明的变量作用域为直接函数体,而 let
变量作用域为 { } 表示的直接封闭块。
来自docs:
The block statement is often called 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. The opposite behavior is possible using an empty statement, where you provide no statement, although one is required.
Blocks are commonly used in association with if...else
and for
statements.
根据你的例子:
function f() {
const z = 3
const z = 300
console.log('z outside of block scope is', z)
}
f()
如果不使用块作用域,我们会得到如下错误:
SyntaxError: redeclaration of const z
并且具有块作用域:
function f() {
const z = 3
{
const z = 300
console.log('z in block scope is', z)
}
console.log('z outside of block scope is', z)
}
f()
相同的代码工作正常。请注意,块作用域 const z = 300
不会抛出 SyntaxError 因为它可以在块内唯一地声明。
我正在阅读 pragmatists 的 ES6 新特性。但正如您所看到的,他们在该函数中使用了 匿名块 。 有人可以解释一下那是什么意思。它是任何 javascript 对象还是什么?我们如何使用它?还请为此提供一些参考。
function f() {
var x = 1
let y = 2
const z = 3
{
var x = 100
let y = 200
const z = 300
console.log('x in block scope is', x)
console.log('y in block scope is', y)
console.log('z in block scope is', z)
}
console.log('x outside of block scope is', x)
console.log('y outside of block scope is', y)
console.log('z outside of block scope is', z)
}
f()
这只是一个光秃秃的街区。任何时候遇到裸块,你都可以认为它等同于:
for (let i = 0; i < 1; i++) {
// block contents here
}
或
if (true) {
// block contents here
}
没什么特别的,也很少用到。它最重要的部分可能是如何在其中声明的 const
和 let
变量的作用域 仅限于块 ,而不是外部函数。
它与 Javascript 中的所有非功能块具有相同的行为 - 就像 if
块、while
块或 for
块,只是没有条件进入,正好执行一次
匿名块在使用 let 变量时很有用。 var
关键字声明的变量作用域为直接函数体,而 let
变量作用域为 { } 表示的直接封闭块。
来自docs:
The block statement is often called 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. The opposite behavior is possible using an empty statement, where you provide no statement, although one is required.
Blocks are commonly used in association with
if...else
andfor
statements.
根据你的例子:
function f() {
const z = 3
const z = 300
console.log('z outside of block scope is', z)
}
f()
如果不使用块作用域,我们会得到如下错误:
SyntaxError: redeclaration of const z
并且具有块作用域:
function f() {
const z = 3
{
const z = 300
console.log('z in block scope is', z)
}
console.log('z outside of block scope is', z)
}
f()
相同的代码工作正常。请注意,块作用域 const z = 300
不会抛出 SyntaxError 因为它可以在块内唯一地声明。