方法一:使用原型链
function Animal(legsNumber){
this.legsNumber = legsNumber
}
Animal.prototype.kind = '动物'
function Dog(name){
Animal.call(this, 4)
this.name = name
}
var F = function(){}
F.prototype = Animal.prototype
Dog.prototype = new F()
Dog.prototype.say = function(){
console.log(`汪汪汪~ 我是${this.name},我有${this.legsNumber}条腿。`)
}
Dog.prototype.kind = '狗'
const d1 = new Dog("啸天")
d1.say()
const d2 = new Dog("啸天2")
d2.say()
console.log(d2);
方法二:使用 class
class Animal{
constructor(legsNumber){
this.legsNumber = legsNumber
}
run(){}
}
class Dog extends Animal{
constructor(name) {
super(4)
this.name = name
}
say(){
console.log(`汪汪汪~ 我是${this.name},我有${this.legsNumber}条腿。`)
}
}