Types of Properties--Data Properties

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:

[[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:"尼古拉斯"

};

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.

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:

var person={};

Object.defineProperty(person,'name',{

configurable:false,

value:"Nicholas"

});

//throws an error

Object.defineProperty(person,'name',{

configurable:true,

value:'NIcholas'

});


So although you can call Object.defineProperty() multiple times for the same property,there are limits once configurable has been set to false.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值