<script>
var boy_proto={
get sex(){ //获取
if(this.sexs=="man"){
console.log("性别:"+this.sexs);
}else{
console.log("性别:"+this.sexs);
}
},
set sex(value){ //设置
this.sexs=value;
}
}
var boy={
name:"joe",
sexs:"man",
__proto__:boy_proto
}
boy.sexs="girl";
boy.sex="ddd";
boy.sex; //性别:ddd
</script>
JS访问器(getter和setter)
最新推荐文章于 2024-08-07 18:44:07 发布