这段代码中,为什么year声明的时候前面加了下划线,用的时候却没加,我尝试将声明处的下划线去掉,结果程序执行不了,求解释。var book = {_year:2004,edition:1};Object.defineProperty(book,"year",{get:function(){return this._year;},set:function(newValue){if(newValue>2004){this._year = newValue;this.edition += newValue - 2004;}}});book.year = 2005;alert(book.edition);
1 回答
墨色风雨
TA贡献1853条经验 获得超6个赞
Object.defineProperty(book,"year",{ get:function(){ return this._year; }, set:function(newValue){ if(newValue>2004){ this._year = newValue; this.edition += newValue - 2004; } }}); |
注意看这个方法 defineProperty 是给book添加一个year的属性
这个属性获取值的方法是 get 设置值的方法是set 和上面的_year无关
那么book 就有了year 和_year2个属性的
添加回答
举报
0/150
提交
取消
