大家好,我是龙骑士,今天给大家讲一下JS的继承
先定义一个父类,提供继承属性
//Father有两个属性name和sum
function Father(name,value){
this.name=name;
this.sum=function(){
return this.value=value+3;
}
}
Father.prototype.age=40;
原型链继承
function Son(){
this.name='ker';
}
Son.prototype=new Father();
var son1=new Son();
console.log(son1 instanceof Father);
构造函数继承
function Son(){
Father.call(this,'longqi');
this.age=22;
}
var son1=new Son();
console.log(son1 instanceof Father);
组合继承
//组合继承
function Son(){
Father.call(this,name);
//借用构造函数模式
this.age=20;
}
Son.prototype=new Father();
//原型链模式
除了上面三种还有原型式继承,寄生式继承,寄生组合式继承,大家可以自己去探索
- 原型式继承
- 寄生式继承
- 寄生组合式继承