- 使用getter和setter
class Animal {
_name = 'dog';
get name(){
return this._name;
}
set name(val){
if(val){
this._name = val;
}else{
return false;
}
}
}
let cat = new Animal();
cat.name = 'cat';
console.log(cat);
class Animal {
_name = 'dog';
get name(){
return this._name;
}
set name(val){
if(val){
this._name = val;
}else{
return false;
}
}
}
let cat = new Animal();
cat.name = 'cat';
console.log(cat);
本文标题:ES6学习(12)class中的getter/setter
本文链接:https://www.haomeiwen.com/subject/saphurtx.html
网友评论