实现类
function Dog(name){
this.name = name
this.legsNumber = 4
}
Dog.prototype.kind = '狗'
Dog.prototype.say = function(){
console.log(`汪汪汪~ 我是${this.name},我有${this.legsNumber}条腿。`)
}
const dog1 = new Dog('哈士奇')
dog1.say()
class Dog{
kind = '狗'
constructor(name){
this.name = name
this.legsNumber = 4
}
say(){
console.log(`汪汪汪~ 我是${this.name},我有${this.legsNumber}条腿。`)
}
}
const dog1 = new Dog('哈士奇')
dog1.say()
实现继承
function Animal(legsNumber){
this.legsName = legsName
}
Animal.proptype.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}条腿。`)
}
const dog1 = new Dog('哈士奇')
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}条腿。`)
}
}