访问器属性不包含 数值 ,包含setter和getter函数
var book={
_year:2004, //只能通过对象方法访问的属性
edition:1
};
//IE9+,Firefox4+,Safari5+,Opera12+,Chrome
Object.defineProperty(book,"year",{
get: function(){
return this._year;
},
set:function(newValue){
if(newValue>2004){
this._year=newVlaue);
this.edition +=newVlaue -2004;
}
});
//以前版本 定义访问器的旧方法
book._defineGetter_("year",function(){
return this._year;});
book._defineSetter_("year",function(newValue){
if(newValue>2004){
this._year=newVlaue);
this.edition +=newVlaue -2004;
}
});
book.year=2005;
alert(book.edition); //2
为对象定义多个属性 Object.degineProperties(){book,{多个属性}}
读取属性特性 Object.getOwnPropertyDescriptor(nook,"year');