使用原型链
function Animal(legsNumber) {
this.legsNumber = legsNumber;
}
Animal.prototype.kind = "动物";
function Dog(name) {
this.name = name;
Animal.call(this, 4);
}
Dog.prototype._proto_ = Animal.prototype;
Dog.prototype.kind = "狗";
Dog.prototype.say = function () {
console.log(`汪汪汪~ 我是${this.name},我有${this.legsNumber}条腿。`);
};
Dog.prototype._proto_ = Animal.prototype;
const f = function () {};
f.prototype = Animal.prototype;
Dog.prototype = new f();
使用Class
class Animal {
constructor(legsNumbers) {
this.legsNumbers = legsNumbers;
}
run() {}
}
class Dog extends Animal {
constructor(name) {
super(4);
this.name = name;
}
say() {
console.log("只因你太美");
}
}