继承发展史
1.传统形式-->原型链
Father.prototype.lastName="小明";
function Father(){};
Son.prototype=new Father();
function Son (){};
var son=new Son();
过多的继承了没用的属性(全部继承下来)
2.借用构造函数
Father.prototype.lastName="小明";
function Father(ko){
this.mm=ko;
};
function Son (){Father.call(this,"消防")};
var son=new Son();
不能继承借用构造函数的原型
每次构造函数都要都走一个函数
3.共享原型
Father.prototype.lastName="小明";
function Father(){};
Son.prototype=Father.prototype;
function Son(){};
var son =new Son();
var father=new Father();
不能随便改动自己的原型
4.圣杯模式(就比上面多添加了F)
// Father.prototype
//
// function F(){}
// F.prototype=Father.protopyte
// Son.prototype=new F();
//Father Son
//圣杯模式
function inherit(Target,Origin){
function F(){};
F.prototype=Origin.prototype;
Target.prototype=new F();
Target.prototype.constuctor=Target;
Target.prototype.uber=Origin.prototype;
}
Father.prototype.lastName="小米功能";
function Father(){
}
function Son(){
}
inherit(Son,Father);
var son=new Son();
var father=new Father();
son.__proto__-->new F().__proto__-->Fanher.prototype
即使被new了F里面也没有什么东西可运行的,除了原型,所以他是最终的模型
var inhert=(function (){
var F=function(){};
return function (Targer,Origin){
F.prototype=Origin.prototype;
Targer.prototype=new F();
Targer.prototype.constructor=Targer;
Targer.prototype.uber=Origin.prototype;
}
}());
inhert(Son,Father);
Father.prototype.lastName="小爱好";
function Father(){
}
function Son(){}
var son=new Son();
练习
function inherit(Target,Origin){
function F(){};
F.prototype=Origin.prototype;
Target.prototype=new F();
Target.prototype.constuctor=Target;
Target.prototype.uber=Origin.prototype;
}
Father.prototype.lastName="小米功能";
function Father(){}
function Son(){
}
inherit(Son,Father);
var son=new Son();
var father=new Father();
var inhert1=(function (){
var F=function(){};
return function (Targer,Origin){
F.prototype=Origin.prototype;
Targer.prototype=new F();
Targer.prototype.constructor=Targer;
Targer.prototype.uber=Origin.prototype;
}
}());
inhert1(Son1,Father1);
Father1.prototype.lastName="小爱好";
function Father1(){
}
function Son1(){}
var son1=new Son1();