JS 对象的访问器属性setter getter函数

来源:互联网 发布:网络举报赌博 编辑:程序博客网 时间:2024/05/10 01:44

访问器属性不包含 数值  ,包含setter和getter函数

var book={_year:2004,  //只能通过对象方法访问的属性edition:1};//IE9+,Firefox4+,Safari5+,Opera12+,ChromeObject.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');

0 0