var man={
weibo:"son",
$age:null,
get age(){
if(this.$age == undefined){
return new Date().getFullYear()-1988;
}else{
return this.$age;
}
},
set age(val){
if(!isNaN(val)&&val>0&&val<150){
this.$age=val;
}else{
throw new Error('Incorrect age:'+val);
}
}
}
console.log(man.age);
man.age=100;
console.log(man.age);
man.age='abc';
JavaScript set/get
最新推荐文章于 2023-09-27 17:49:58 发布