JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

3.10.2 Variables As Properties

3.10.2 Variables As Properties

When you declare a global JavaScript variable, what you are actually doing is defining a property of the global object (§3.5 ). If you use varto declare the variable, the property that is created is nonconfigurable (see §6.7 ), which means that it cannot be deleted with the delete operator. We’ve already noted that if you’re not using strict mode and you assign a value to an undeclared variable, JavaScript automatically creates a global variable for you. Variables created in this way are regular, configurable properties of the global object and they can be deleted:

var truevar = 1; // A properly declared global variable, nondeletable. fakevar = 2; // Creates a deletable property of the global object. this.fakevar2 = 3; // This does the same thing. delete truevar // => false: variable not deleted delete fakevar // => true: variable deleted delete this.fakevar2 // => true: variable deleted

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, 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 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, 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, and it is developed further in the next section.

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1