这里有一段示例代码
var book = {};
Object.defineProperties(book, {
_year: {
value: 2004
},
edition: {
value: 1
},
year: {
get: function(){
console.log('Getting year');
return this._year;
},
set: function(newValue){
console.log('Setting year');
console.log('this:',this);
if (newValue > 2004) {
this._year = newValue;
this.edition ++;
}
}
}
});
console.info(book);
console.log('year:'+ book.year);
book.year = 2008;
console.log('edition:'+ book.edition);
但是在执行book.year赋值调用到setter方法中时,this.edition ++
虽然执行了但是没有生效:book.edition
值比并没有被改变,也查看了this的引用也没问题。这是为什么?