Uncaught RangeError: Maximum call stack size exceeded

Litwak 2017-06-02 12:07:33
js代码:
    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);
...全文
419 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
2017-06-02
  • 打赏
  • 举报
回复
set里面又改了year的值,无限循环了

87,921

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧