我将set age()和属性age:null设置为同名,结果报错:Maximum call stack size exceeded,只要改成不同名就ok,请问这是什么原因?
var man = { name:"nrt", sex:"female", age:null, get age(){ if(this.age == undefined) { return new Date().getFullYear() - 1995; } else { return this.age; } }, set age(val){ val = +val //转换为数值类型 if(!isNaN(val) && val > 0 && val < 150) { this.age = +val; } else { console.log("输入非法"); } } } console.log(man.age); man.age = 30; console.log(man.age);