一戏蔑、原型鏈繼承
function sub(){
this.name='張新苗';
}
sub.prototype= new Person();
var s1=new sub();
console.log(s1);
console.log(s1 instanceof Person);//檢測(cè)是否繼承
二蹋凝、構(gòu)造函數(shù)的繼承
function sub(){
Person.call(this,'章繪繪');//改變this指向
}
var s1=new sub();
console.log(s1);
三、組合式繼承
function sub(){
Person.call(this,'張玉芯');//借用構(gòu)造函數(shù)的方式
}
sub.prototype=new Person();//原型鏈的繼承
var s1=new sub();//繼承
console.log(s1);
四总棵、原型式繼承
function content(obj){
function F(){}
F.prototype=obj;//繼承傳入的參數(shù)
return new F();//返回實(shí)例對(duì)象
}
var s1=new Person();//把Person實(shí)例的方法拿過(guò)來(lái)
var s2=content(s1);
console.log(s2);
五鳍寂、寄生式繼承
function content(obj){
function F(){}
F.prototype=obj
return new F();
}
var s1=new Person();//Person的方法,原型式繼承
function sub(obj){//再套個(gè)盒子傳遞參數(shù)
var s2=content(obj);
s2.name='曹慧潔'
return s2;
}
var s3=sub(s1)//所以這個(gè)時(shí)候sub就有Person的方法了
console.log(s3.age);
六情龄、寄生組合方法封裝的繼承
function content(obj){
function F(){};
F.prototype=obj;
return new F();
}
console.log(content())//指向F{}
console.log(Person.prototype)//指向Person(name)
var c=content(Person.prototype); //把Person的方法給放進(jìn)去
function sub(){
Person.call(this)//改變this指向
}
sub.prototype=c;
var s2=new sub();
console.log(s2);
寄生組合方法簡(jiǎn)單化的繼承
console.log(Person.prototype);//{constructor: ?} 對(duì)象
function F(){}
console.log(F.prototype)//{constructor: ?} 對(duì)象
F.prototype=Person.prototype
function sub(){
Person.call(this)//改變this指向
}
console.log(sub.prototype.constructor)//指向sub函數(shù)
sub.prototype=new F();//把Person的方法拿來(lái)
console.log(sub.prototype)
var s2= new sub()//sub里有Person的方法
console.log(s2);