继承

构造函数形式的继承

1
2
3
4
5
6
7
8
9
10
11
12
13
function Animal(){
this.eat=function(){
console.log('animal eat')
}
}
function Dog(){
this.bark=function(){
console.log('Dog bark')
}
}
Dog.prototype=new Animal()
var dog1=new Dog()
dog1.eat()

class继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Animal{
constructor(name){
this.name=name
}
eat(){
console.log(this.name+'eat')
}
}
class Dog extends Animal(){
constructor(name,age){
super(name)//继承父类
this.age=age
}
say(){
console.log(this.name+'say')
}
}
const dog=new Dog('哈士奇')
dog.say()

拷贝继承

1
//高程6.3继承