87,921
社区成员
发帖
与我相关
我的任务
分享
var person = {//数据属性
year:2016,
edition:1
}
Object.defineProperty(person,"year",{//访问器属性
get:function () {
return this.year;
},
set:function (newValue) {
if(newValue > 2017){
this.year = newValue;
this.edition += newValue - 2017;
}
}
});
person.year = 2019;
console.log(person.edition);
console.log(person.year);
Uncaught RangeError: Maximum call stack size exceeded
var person = {//数据属性
//_year:2017,
edition:1
}
Object.defineProperty(person,"year",{//访问器属性
get:function () {
return this._year;
},
set:function (newValue) {
if(newValue > 2017){
this._year = newValue;
this.edition += newValue - 2017;
}
}
});
person.year = 2019;
console.log(person._year);
console.log(person.edition);//根据year设置版本号
console.log(person.year);