Do let语句在全局对象上创建属性

Do let statements create properties on the global object?

本文关键字:创建 属性 对象 全局 let 语句 Do      更新时间:2024-06-23

在JavaScript中,var声明在全局对象上创建属性:

var x = 15;
console.log(window.x); // logs 15 in browser
console.log(global.x); // logs 15 in Node.js

ES6通过具有块作用域的let声明引入了词法作用域。

let x = 15;
{
   let x = 14;
}
console.log(x); // logs 15;

但是,这些声明是否会在全局对象上创建属性?

let x = 15;
// what is this supposed to log in the browser according to ES6?
console.log(window.x); // 15 in Firefox
console.log(global.x); // undefined in Node.js with flag

let语句是否在全局对象上创建属性?

根据规范,编号:

全局环境记录在逻辑上是单个记录,但它被指定为封装对象环境记录和声明性环境记录的组合。对象环境记录将关联领域的全局对象作为其基础对象。此全局对象是全局环境记录的GetThisBinding具体方法返回的值。全局环境记录的对象环境记录组件包含所有内置全局的绑定(第18条),以及全局代码中包含的FunctionDeclarationGeneratorDeclarationVariableStatement引入的所有绑定全局代码中所有其他ECMAScript声明的绑定都包含在全局环境记录的声明性环境记录组件中

更多解释:

  • 声明性环境记录将绑定存储在内部数据结构中。以任何方式都不可能掌握该数据结构(想想函数范围)。

  • 对象环境记录使用实际的JS对象作为数据结构。对象的每个属性都成为绑定,反之亦然。全局环境有一个对象环境对象,其"绑定对象"是全局对象。另一个例子是CCD_ 5。

现在,正如所引用的部分所述,只有FunctionDeclarations、GeneratorDeclarations和VariableStatements在全局环境的对象环境记录中创建绑定。也就是说,只有这些绑定成为全局对象的属性。

所有其他声明(例如constlet)都存储在全局环境的声明性环境记录中,该记录不是基于全局对象的。

标准脚本:

如果在脚本的顶层声明letvar变量,则它们都可以在脚本文件之外访问。但是,只有var变量被分配给window对象。看看这个代码片段作为证据:

<script>
  var namedWithVar = "with var";
  let namedWithLet = "with let";
</script>
<script>
  console.log("Accessed directly:");
  console.log(namedWithVar);        // prints: with var
  console.log(namedWithLet);        // prints: with let
  console.log("");
  console.log("Accessed through window:");
  console.log(window.namedWithVar); // prints: with var
  console.log(window.namedWithLet); // prints: undefined
</script>

Javascipt模块:

请注意,模块是另一回事。模块中声明的变量在全局范围内不可用:

<script type="module">
  var namedWithVar = "with var";
  let namedWithLet = "with let";
</script>
<script>
  console.log(namedWithVar); // ReferenceError
</script>
<script>
  console.log(namedWithLet); // ReferenceError
</script>
<script>
  console.log(window.namedWithVar); // prints: undefined
  console.log(window.namedWithLet); // prints: undefined
</script>

根据规范:

"let和const声明定义了作用域为正在运行的执行上下文的LexicalEnvironment的变量。"

这意味着您应该能够在执行范围内访问变量,但不能在执行范围外访问。这将执行范围扩展到了仅函数或全局的经典JS闭包结构之外。

全局定义let变量不应像Firefox中那样在全局上下文中公开该变量。在实践中,您不应该在全局上下文中定义变量。

通过let关键字声明的变量不会在全局对象上创建可访问的属性(浏览器的窗口)。实际上,Firefox修复了它的行为:let v = 42; 'v' in window // false

let允许您声明范围仅限于使用它的块、语句或表达式的变量。这与var关键字不同,后者全局定义变量,或局部定义整个函数的变量,而不考虑块范围。

在程序和函数的顶级,let与var不同,不在全局对象上创建属性。例如:

   var x = 'global';
   let y = 'global';
   console.log(this.x); // "global"
   console.log(this.y); // undefined

var声明的变量的作用域是其当前执行上下文,该上下文是封闭函数,对于在任何函数之外声明的变量,它是全局的。如果您重新声明一个JavaScript变量,它不会丢失其值。例如:

var x = 1;
if (x === 1) {
  var x = 2;
  console.log(x);
  // output: 2
}
console.log(x);
// output: 2

注意:CC++Java不同,当您使用var声明变量时,JavaScript不具有块级作用域。

正如我们在let之前所提到的,允许您声明在其使用的块、语句或表达式范围内受限的变量。例如:

let x = 1;
if (x === 1) {
  let x = 2;
  console.log(x);
  // output: 2
}
console.log(x);
// output: 1

在这里,我建议您阅读有关可变范围