让let语句在全局对象上创建属性? [英] Do let statements create properties on the global object?
问题描述
var x = 15;
console.log(window.x); // logs 15 in browser
console.log(global.x); //在Node.js中记录15
ES6引入词汇范围与 let
声明具有阻止范围。
let x = 15;
{
let x = 14;
}
console.log(x); //日志15;
但是,这些声明是否会在全局对象上创建属性?
let x = 15;
//这是什么应该根据ES6登录浏览器?
console.log(window.x); // 15 in Firefox
console.log(global.x); //未定义在Node.js中,标志为
do
let
语句在全局对象上创建属性?
根据规格,no:
全局环境记录在逻辑上是单个记录,但它被指定为封装对象环境记录和声明性环境记录。 对象环境记录具有作为其基础对象的全局对象关联的领域。这个全局对象是全局环境记录的
GetThisBinding
具体方法返回的值。全局环境记录的对象环境记录组件包含所有内置全局变量的绑定(条款18 )以及全局代码中包含的 FunctionDeclaration , GeneratorDeclaration 或 VariableStatement 引入的所有绑定。 全局代码中所有其他ECMAScript声明的绑定包含在中
有些更多的解释:
-
声明式环境记录将绑定存储在内部数据结构中。
-
对象环境记录使用的是不可能以任何方式掌握该数据结构(考虑功能范围)一个实际的JS对象作为数据结构。对象的每个属性变成绑定,反之亦然。全局环境有一个对象环境对象,其绑定对象是全局对象。另一个例子是
与
。
部分状态,只有功能声明 s, GeneratorDeclaration s和 VariableStatement 在全局环境的对象环境记录中创建绑定。即只有这个绑定成为全局对象的属性。
所有其他声明(例如 const
和 let
)存储在全局环境的声明性环境记录中,而不是基于全局对象。
In JavaScript, var
declarations create properties on the global object:
var x = 15;
console.log(window.x); // logs 15 in browser
console.log(global.x); // logs 15 in Node.js
ES6 introduces lexical scoping with let
declarations that have block scope.
let x = 15;
{
let x = 14;
}
console.log(x); // logs 15;
However, do these declarations create properties on the global object?
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
Do
let
statements create properties on the global object?
According to the spec, no:
A global environment record is logically a single record but it is specified as a composite encapsulating an object environment record and a declarative environment record. The object environment record has as its base object the global object of the associated Realm. This global object is the value returned by the global environment record’s
GetThisBinding
concrete method. The object environment record component of a global environment record contains the bindings for all built-in globals (clause 18) and all bindings introduced by a FunctionDeclaration, GeneratorDeclaration, or VariableStatement contained in global code. The bindings for all other ECMAScript declarations in global code are contained in the declarative environment record component of the global environment record.
Some more explanation:
A declarative environment record stores the bindings in an internal data structure. It's impossible to get a hold of that data structure in any way (think about function scope).
An object environment record uses an actual JS object as data structure. Every property of the object becomes a binding and vice versa. The global environment has an object environment object whose "binding object" is the global object. Another example is
with
.
Now, as the cited part states, only FunctionDeclarations, GeneratorDeclarations, and VariableStatements create bindings in the global environment's object environment record. I.e. only this bindings become properties of the global object.
All other declarations (e.g. const
and let
) are stored in the global environment's declarative environment record, which is not based on the global object.
这篇关于让let语句在全局对象上创建属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!