关于类,子类和父类共享一个this,需要调用super( ),否则会报错

class Animal{
    constructor(){
        this.name='miaomiao';
    }
    getName(){
        return this.name;
    }
}

class Cat extends Animal{
    constructor(){
        super();
        this.name='haha';
    }
}
var animal=new Animal();
var cat=new Cat();
console.log(animal.getName());
console.log(cat.getName());

猜你喜欢

转载自blog.csdn.net/gloria199091/article/details/80177368