📜  javascript 类继承 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:17.249000             🧑  作者: Mango

代码示例1
// Class Inheritance in JavaScript
class Mammal {
    constructor(name) {
        this.name = name;
    }
    eats() {
        return `${this.name} eats Food`;
    }
}

class Dog extends Mammal {
    constructor(name, owner) {
        super(name);
        this.owner = owner;
    }
    eats() {
        return `${this.name} eats Chicken`;
    }
}

let myDog = new Dog("Spot", "John");
console.log(myDog.eats()); // Spot eats chicken