ECMA-262 fifth edition describes characteristics of properties through the use of internal-only attributes.These attributes are defined by the specification for implementation in JavaScript engines, and as such, these attributes are not directly accessible in JavaScript.To indicate that an attribute is internal, surround the attribute name with two pairs of square brackets,such as [[Enumerable]]. Although ECMA-262 third edition had different definitions , this book refers only to the fifth edition descriptions.
There are two types of properties:data properties and accessor properties.
Data Properties
Data Properties contain a single location for a data value. Values are read from and written to this location.Data properties have four attributes describing their behavior:
When a property is explicitly added to an object as in the previous examples, [[Configurable]],[[Enumerable]],and [[Writable]] are all set to true while the [[Value]] attribute is set to the assigned value.[[Configurable]] Indicates if the property may be redefined by removing the property via delete,changing the property's attributes,or changing the property into an accessor property.By default ,this is true for all properties defined directly on an object.
[[Enumerable]] Indicates if the property will be returned in a for-in loop.By default ,this is true for all properties defined directly on an object.
[[Writable]] Indicates if the property's value can be changed .By default ,this is true for all properties defined directly on an object.
[[Value]] Contains the actual data value for the property. This is the location from which the property's value is read and the location to which new values are saved.The default value for this attribute is undefined
var person={
name:"尼古拉斯"
};
Here,the property called name is created and a value of '尼古拉斯',and any changes to that value are stored in this location.
To change any of the default property attributes,you must use the ECMAScript 5 Object.defineProperty() method. This method accepts three arguments:the object on which the property should be added or modified ,the name of the property ,and a descriptor object. The properties on the descriptor object match the attribute names:configurable,enumerable,and value. You can set one or all of these values to change the corresponding attribute values. For example:
var person={};
Object.defineProperty(person,'name',{
writable:false,
value:'Nicholas'
});
alert(person.name);// 'Nicholas'
person.name="Greg";
alert(person.name);// 'Nicholas'
This example creates a property called name with a value of 'Nicholas' that is read-only.The value of this property can't be changed,and any attempts to assign a new value are ignored in nonstrict mode. In strict mode ,an error is thrown when an attempt is made to change the value of a read-only property.
Similar rules apply to creating a nonconfigurable property.For example:
var person={};
Object.defineProperty(person,'name',{
configurable:false,
value:'Nicholas'
});
alert(person.name);//'Nicholas'
delete person.name;
alert(person.name);//'Nicholas'
Here,setting configurable to false means that the property cannot be removed from the object.Calling delete on the property has no effect in nonstrict mode and throws an error in strict mode.Additionally,once a property has been defined as nonconfigurable,it cannot become configurable again.Any attempt to call Object.defineProperty() and change any attribute other than writable causes an error:
So although you can call Object.defineProperty() multiple times for the same property,there are limits once configurable has been set to false.var person={};
Object.defineProperty(person,'name',{
configurable:false,
value:"Nicholas"
});
//throws an error
Object.defineProperty(person,'name',{
configurable:true,
value:'NIcholas'
});
When you are using Object.defineProperty(),the values for configurable,enumerable,and writable default to false unless otherwise specified. In most cases,you likely won't need the powerful options provided by Object.defineProperty(),but it's important to understand the concepts to have a good understanding of JavaScript objects.
参考文献:Professional JavaScript for Web Developers,third edition