object
Objects have properties,
Attribute types
Data attributes and accessor attributes
Data attribute
Data attributes contain data values: Value
The characteristics of data attributes (used to describe the behavior of data attributes):
[[Configurable]]:
- Can we pass? delete Delete attributes to redefine attributes
- Can you modify attributes?Characteristic
- Can you change attributes to accessor attributes?
[[Enumerable]]
-Indicate whether it can pass for-in Loop return properties
[[Writable]]
-Can you modify attributes?value
[[Value]]
-The data value containing this property is the default read-write location of the property undefined
Modified features:
Object.defineProperty() method: -(ES5)
Object.defineProperty(obj, "name", [descriptor object})
var person = {};
Object.defineProperty(person, "name", {
writable: false,//Characters cannot modify the value of attributes
value: "Nicholas"//The feature book is read-write but read-only because of writable:false
/*descriptor object*/
});
alert(person.name); //"Nicholas"
person.name = "Greg";
alert(person.name); //"Nicholas"
/*Elevation Code Data Properties Example01.htm [p140]*/
var person = {};
Object.defineProperty(person, "name", {
configurable: false,
//- Attributes cannot be redefined by deleting attributes (1)
//- Property that *** cannot modify attributes *** (2)
//- Attributes cannot be changed to accessor attributes (3)
value: "Nicholas"
});
//Throw wrong
Object.defineProperty(person, "name", {
configurable: true,//Because (2) Correct the error report again at this time
value: "Nicholas"
});
/*When calling the Object.defineProperty() method, if not specified, the default values for configurable, enumerable, and writable features are false*/
/*All three default values are true when setting properties directly on objects*/
IE8 is the first browser version to implement the Object.defineProperty() method. However, this
Version implementations have many limitations: you can only use this method on DOM objects, and you can only create accessors
Attribute. Due to the incomplete implementation, the reader is advised not to use Object.defineProperty() in IE8.
Method. —————————————————— - Elevation [P141]
Accessor properties
Accessor properties do not contain data values: (no Value)
Read: getter() function
Write: setter() function
The characteristics of accessor attributes:
[[Configurable]]:
- Can you redefine attributes by deleting attributes
- Can you modify the properties of attributes?
- Can you modify attributes to data attributes (comparing the characteristics of data attributes)
- Default to ture
[[Enumerable]]
- Represents the ability to return attributes directly defined on an object through a for-in loop, which
The default value for each feature is true.
[[Get]]
- read property default undefined
[[Set]]
- Set the default undefined property
Use Object.defineProperty() to define
var book = {
_year: 2004,//Represents properties that can only be accessed through object methods
edition: 1
};
Object.defineProperty(book, "year", {
//Year is an accessor attribute, affecting the _year and edition attributes
get: function(){
return this._year;//Returns the value of the _year attribute
},
set: function(newValue){
if (newValue > 2004) {
this._year = newValue;//Revaluation_year
this.edition += newValue - 2004;//Relevance processing edition
}
}
});
book.year = 2005;
alert(book.edition); //2
//Elevation - Accessor Properties Example01. HTM [p141]
The browsers that support ECMAScript 5 include IE9+ (IE8 is only partially implemented), Firefox 4+, Safari 5+, Opera 12+, and Chrome.
Before this method, two non-standard methods are commonly used to create accessor attributes: _defineGetter_() and _defineSetter_(). These two methods were first introduced by Firefox, and later Safari 3, Chrome 1, and Opera 9.5 gave the same implementation.
var book = {
_year: 2004,
edition: 1
};
//Old methods for defining accessors
book.__defineGetter__("year", function(){
return this._year;
});
book.__defineSetter__("year", function(newValue){
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004;
}
});
book.year = 2005;
alert(book.edition); //2
//AccessorPropertiesExample02.htm[p142]
Define multiple attributes
Object.defineProperties() method.
var book = {};
Object.defineProperties(book, {
_year: {
value: 2004
},
edition: {
value: 1
},
year: {
get: function(){
return this._year;
},
set: function(newValue){
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004;
}
}
}
});
//MultiplePropertiesExample01.htm
Features of read attributes
Object.getOwnPropertyDescriptor() method
Two parameters are included:
Objects where attributes are located
The property name of the descriptor to read.
Return value: An object.
If it is an accessor property,
Attributes are configurable, enumerable, get, and set.
If it is a data attribute,
Attributes are configurable, enumerable, writable, and value
var book = {};
Object.defineProperties(book, {//Define multiple attributes
_year: {
value: 2004
},
edition: {
value: 1
},
year: {
get: function() {
return this._year;
},
set: function(newValue) {
if(newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004;
}
}
}
});
var descriptor = Object.getOwnPropertyDescriptor(book, "_year");//Getting descriptor objects
alert(descriptor.value); //2004
alert(descriptor.configurable); //false
alert(typeof descriptor.get); //"undefined"
var descriptor = Object.getOwnPropertyDescriptor(book, "year");
alert(descriptor.value); //undefined
alert(descriptor.enumerable); //false
alert(typeof descriptor.get); //"function"
//GetPropertyDescriptorExample01.htm[p143-144]
Object.getOwnPropertyDescriptor() method