原型继承:
1.构造函数继承
在子类构造函数中,执行父类的构造函数,实现对父类构造函数的复用。
2.类式继承
在子类的原型上,实例化父类,实现对父类的原型继承。
3.组合继承
综合使用构造函数继承和类式继承
构造函数继承
1 //父类 2 var Person=function(name){ 3 this.name=name; 4 } 5 Person.prototype={ 6 age:18, 7 sayHello:function(){ 8 console.log('hi,i am ',this.name); 9 } 10 }; 11 12 //子类 13 var Chinese=function(name,phone){ 14 Person.call(this,name); 15 this.phone=phone; 16 } 17 18 var person1=new Chinese('yangzhi','13333333333'); 19 console.log(person1);
类式继承
1 //父类 2 var Person=function(name){ 3 this.name=name; 4 } 5 Person.prototype={ 6 age:18, 7 sayHello:function(){ 8 console.log('hi,i am ',this.name); 9 } 10 }; 11 12 //子类 13 var Chinese=function(name,phone){ 14 this.name=name; 15 this.phone=phone; 16 } 17 Chinese.prototype=new Person(); 18 var person1=new Chinese('yangzhi','13333333333'); 19 console.log(person1);
组合继承
1 //父类 2 var Person=function(name){ 3 this.name=name; 4 } 5 Person.prototype={ 6 age:18, 7 sayHello:function(){ 8 console.log('hi,i am ',this.name); 9 } 10 }; 11 12 //子类 13 var Chinese=function(name,phone){ 14 Person.call(this,name); 15 this.phone=phone; 16 } 17 Chinese.prototype=new Person(); 18 var person1=new Chinese('yangzhi','13333333333'); 19 console.log(person1);