ECMAScript6新特性之继承

class Animal{
    constructor(name){
        this.name = name;
    }
    get name(){
        return this._name;
    }
    set name(val){
        this._name = val;
    }
    run(area){
        console.log(`在${area}奔跑`);
    }
    static hunt(food){
        console.log(`捕食${food}`);
    }
}
class Lion extends Animal{
    constructor(name,desc){
        super(name);
        this._desc = desc;
    }
    get desc(){
        return this._desc;
    }
    set desc(val){
        this._desc = val;
    }
}
let lion = new Lion('非洲雄狮','大型猫科动物');
console.log('lion',lion);
lion.run('非洲草原');
Lion.hunt('野牛');

  如果get、set都没有或都有,则属性是可读可写的。

  如果只有get,则属性是只读的,写会抛异常。

  如果只有set,则属性是只写的,读会获得undefined。

原文地址:https://www.cnblogs.com/sea-breeze/p/7001857.html