JS -- Variables As Properties

时间:2023-03-09 16:32:51
JS -- Variables As Properties

Variables As Properties

When you declare a global JavaScript variable, what you are actually doing is defining a property of the global object . If you use var to declare the variable, the

当你声明一个全局变量,实际上,你是给全局对象定义了一个属性。如果你是用 var 声明的变量,

property that is created is nonconfigurable , which means that it cannot be deleted with the delete operator. We’ve already noted that if you’re not using strict mode

所创建的属性是不可配置的,也就是说不能用 delete 操作符 从全局对象中删除掉。在非严格模式下,

and you assign a value to an undeclared variable, JavaScript automatically creates a global variable for you.

给一个尚未声明的变量赋值,JS 会自动创建一个全局变量。

JavaScript global variables are properties of the global object, and this is mandated by the ECMAScript specification. There is no such requirement for local variables,

JS全局变量就是全局对象的属性,这是ECMAScript 规定的。局部变量虽然没有这样的规定,

but you can imagine local variables as the properties of an object associated with each function invocation. The ECMAScript 3 specification referred to this object as

但是局部变量是 函数调时创建的关联对象 的属性,ECMAScript 3 把这个 关联对象叫做 “call object”,

the“call object,” and the ECMAScript 5 specification calls it a “declarative environment record.” JavaScript allows us to refer to the global object with the this keyword,

而 ECMAScript 5  把这个关联对象叫做 “声明环境记录”。JS 允许我们通过  this 关键字 来应用全局对象,

but it does not give us any way to refer to the object in which local variables are stored. The precise nature of these objects that hold local variables is an

但是,没有任何办法去引用这个 调用时创建 的关联对象。实现这个关联对象具体细节,不用我们去关心,

implementation detail that need not concern us. The notion that these local variable objects exist, however, is an important one.

但是,重要的是,要知道有这样的一个关联对象存在。