标签:ror else null this 方法 javascrip err leo isnan
var fe={
name:‘leony‘,
$age:null,
get age(){
if(this.$age == undefined){ //this.$age == undefined 或 this.$age == null
return new Date().getFullYear()-1989;
}else{
return this.$age;
}
},
set age(val){
val = +val; //数字转换,若为非数字会转为NaN;
if(!isNaN(val)&&val>0&&val<150){
this.$age = +val;
}else{
throw new Error("Incorrect val = "+val);
}
}
}
console.log(fe.age)
fe.age=‘aaa‘; //调取 fe 函数的 set 方法
console.log(fe.age)
标签:ror else null this 方法 javascrip err leo isnan
原文地址:http://www.cnblogs.com/leong-min/p/6138589.html