javascript的幾種繼承方法介紹
1.原型鏈繼承:構(gòu)造函數(shù)、原型和實(shí)例的關(guān)系:每個(gè)構(gòu)造函數(shù)都有一個(gè)原型對(duì)象,原型對(duì)象都包含一個(gè)指向構(gòu)造函數(shù)的指針,而實(shí)例都包含一個(gè)指向原型對(duì)象的內(nèi)部指針。確認(rèn)原型和實(shí)例之間的關(guān)系用instanceof。
原型鏈繼承缺點(diǎn):字面量重寫原型會(huì)中斷關(guān)系,使用引用類型的原型,并且子類型還無法給超類型傳遞參數(shù)
function Parent(){ this.name='mike'; } function Child(){ this.age=12; } //兒子繼承父親(原型鏈) Child.prototype=new Parent();//Child繼承Parent,通過原型形成鏈條 var test=new Child(); console.log(test.age); console.log(test.name);//得到被繼承的屬性 //孫子繼續(xù)原型鏈繼承兒子 function Brother(){ this.weight=60; } Brother.prototype=new Child();//繼承原型鏈繼承 var brother=new Brother(); console.log(brother.name);//繼承了Parent和Child,彈出mike console.log(brother.age);//12 console.log(brother instanceof Child);//ture console.log(brother instanceof Parent);//ture console.log(brother instanceof Object);//ture
2.構(gòu)造函數(shù)實(shí)現(xiàn)繼承:又叫偽造對(duì)象或經(jīng)典繼承。
構(gòu)造函數(shù)實(shí)現(xiàn)繼承缺點(diǎn):借用構(gòu)造函數(shù)雖然解決了原型鏈繼承的兩種問題,但沒有原型,則復(fù)用無從談起,所以需要原型鏈+借用構(gòu)造函數(shù)模式。
function Parent(age){ this.name=['mike','jack','smith']; this.age=age; } function Child(age){ Parent.call(this,age);//把this指向Parent,同時(shí)還可以傳遞參數(shù) } var test=new Child(21); console.log(test.age);//21 console.log(test.name); test.name.push('bill'); console.log(test.name);//mike,jack,smith,bill
3.組合繼承:使用原型鏈實(shí)現(xiàn)對(duì)原型屬性和方法的繼承,而通過借用構(gòu)造函數(shù)來實(shí)現(xiàn)對(duì)實(shí)例屬性的繼承。這樣即通過在原型上定義方法實(shí)現(xiàn)了函數(shù)復(fù)用,又保證每個(gè)實(shí)現(xiàn)都有它自己的屬性。
缺點(diǎn):無論什么情況下,都會(huì)調(diào)用兩次超類型構(gòu)造函數(shù),一次是在創(chuàng)建子類型原型的時(shí)候,另一次是在創(chuàng)建子類型原型的時(shí)候,另一次是在子類型構(gòu)造函數(shù)內(nèi)部。
function Parent(age){ this.name=['mike','jack','smith']; this.age=age; } Parent.prototype.run=function(){ return this.name+' are both '+this.age; } function Child(age){ Parent.call(this,age);//給超類型傳參,第二次調(diào)用 } Child.prototype=new Parent();//原型鏈繼承,第一次調(diào)用 var test1=new Child(21);//寫new Parent(21)也行 console.log(test1.run());//mike,jack,smith are both 21 var test2=new Child(22); console.log(test2.age); console.log(test1.age); console.log(test2.run()); //這樣可以使test1和test2分別擁有自己的屬性age同時(shí)又可以有run方法
4.原型式繼承:借助原型可以基于已有的對(duì)象創(chuàng)建新對(duì)象,同時(shí)還不必因此創(chuàng)建自定義類型。它要求必須有一個(gè)對(duì)象可以作為另一個(gè)對(duì)象的基礎(chǔ)。
function object(o){ function F(){}; F.prototype=o; return new F(); } var person={ name:'nicho', friends:['shell','jim','lucy'] } var anotherPerson = object(person); anotherPerson.name = 'Greg'; anotherPerson.friends.push('Rob'); console.log(anotherPerson.friends);//["shell", "jim", "lucy", "Rob"] var yetAnotherPerson = object(person); yetAnotherPerson.name = 'Linda'; yetAnotherPerson.friends.push('Barbie'); console.log(yetAnotherPerson.friends);//["shell", "jim", "lucy", "Rob", "Barbie"] console.log(person.friends);//["shell", "jim", "lucy", "Rob", "Barbie"]
ECMAScript5通過新增Object.create()方法規(guī)范化了原型式繼承,這個(gè)方法接收兩個(gè)參數(shù):一個(gè)用作新對(duì)象原型的對(duì)象和(可選的)一個(gè)為新對(duì)象定義屬性的對(duì)象。
var person2={ name:'nicho', friends:['shell','jim','lucy'] }; var anoP2=Object.create(person2); anoP2.name="Greg"; anoP2.friends.push('Rob'); console.log(anoP2.friends);//["shell", "jim", "lucy", "Rob"] var yetP2=Object.create(person2); yetP2.name="Linda"; yetP2.friends.push('Barbie'); console.log(yetP2.friends);//["shell", "jim", "lucy", "Rob", "Barbie"] console.log(person2.friends);//["shell", "jim", "lucy", "Rob", "Barbie"] /*以這種方式指定的任何屬性都會(huì)覆蓋原型對(duì)象上的同名屬性。*/ var threeP=Object.create(person,{ name:{value:'red'} }); console.log(threeP.name);//red,如果threeP中無name則輸出person2里的name值nicho
5.寄生式繼承:思路與寄生構(gòu)造函數(shù)和工廠模式類似,即創(chuàng)建一個(gè)僅用于封裝繼承過程的函數(shù),該函數(shù)在內(nèi)部以某種方式來增強(qiáng)對(duì)象,最后再像真地是它做了所有工作一樣返回對(duì)象。
function object(o){ function F(){}; F.prototype=o; return new F(); }; function createAnother(o){ var cl=object(o); cl.sayHi=function(){ console.log('hi'); } return cl; }; var person={ name:'nick', friends:['shelby','court','van'] } var anotherPerson=createAnother(person); anotherPerson.sayHi();//hi console.log(anotherPerson.name);//nick console.log(anotherPerson.friends);//["shelby", "court", "van"] /*這個(gè)例子中的代碼基于 person 返回了一個(gè)新對(duì)象—— anotherPerson 。 新對(duì)象不僅具有 person 的所有屬性和方法,而且還有自己的 sayHi() 方法*/
寄生組合式繼承:無論什么情況下,都會(huì)調(diào)用兩次超類型構(gòu)造函數(shù),一次是在創(chuàng)建子類型原型的時(shí)候,另一次是在創(chuàng)建子類型原型的時(shí)候,另一次是在子類型構(gòu)造函數(shù)內(nèi)部,這樣子類型最終會(huì)包含超類型對(duì)象的全部實(shí)例屬性,我們不得不在調(diào)用子類型構(gòu)造函數(shù)時(shí)重寫這些屬性。因此出現(xiàn)了寄生組合式繼承。
6.寄生組合式繼承:借用構(gòu)造函數(shù)來繼承屬性,通過原型鏈的混成形式來繼承方法?;舅悸罚翰槐貫榱酥付ㄗ宇愋偷脑投{(diào)用超類型的構(gòu)造函數(shù)。本質(zhì)上就是使用寄生式繼承來繼承超類型的原型,然后再將結(jié)果指定給子類型的原型。
function SuperType(name){ this.name=name; this.colors=['red','blue','green']; } SuperType.prototype.sayName=function(){ console.log(this.name); } function SubType(name,age){ SuperType.call(this,name); this.age=age; } function object(o){ function F(){}; F.prototype=o; return new F(); }; /*inheritPrototype此函數(shù)第一步是創(chuàng)建超類型原型的一個(gè)副本。第二步是為創(chuàng)建的副本添加constructor屬性, * 從而彌補(bǔ)因重寫原型而失去的默認(rèn)的constructor屬性,第三步將新創(chuàng)建的對(duì)象(副本)賦值給子類型的原型*/ function inheritPrototype(subType,superType){ var prototype=object(superType.prototype);//創(chuàng)建對(duì)象 prototype.constructor=subType;//增強(qiáng)對(duì)象 subType.prototype=prototype;//指定對(duì)象 } inheritPrototype(SubType,SuperType); SubType.prototype.sayAge=function(){ console.log(this.age); } var p=new SubType('xiaoli',24); console.log(p.sayName()); console.log(p.sayAge()); console.log(p.colors)
此方法優(yōu)點(diǎn):只調(diào)用了一次父類SuperType構(gòu)造函數(shù),并且因此避免了在SubType.prototype上面創(chuàng)建不必要的多余的屬性。同時(shí)原型鏈還能保持不變,還能正常使用instanceof和isPrototypeOf();
以上這篇javascript的幾種繼承方法介紹就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
document.write與writeln的輸出內(nèi)容區(qū)別說明
document.write()和document.writeln都是JavaScript向客戶端寫入的方法,writeln是以行方式輸出的,但并不是指頁面實(shí)際效果中的換行,兩種方法在查看源代碼時(shí)才看得出區(qū)別。2010-10-10使用 TypeScript 重新編寫的 JavaScript 坦克大戰(zhàn)游戲代碼
這篇文章主要介紹了使用 TypeScript 重新編寫的 JavaScript 坦克大戰(zhàn)游戲代碼,主要是對(duì)自己近期學(xué)習(xí)TypeScript的一個(gè)小小的總結(jié)實(shí)踐,推薦給小伙伴們,希望大家能夠喜歡。2015-04-04圖解prototype、proto和constructor的三角關(guān)系
在javascript中,prototype、constructor以及__proto__之間有著“著名”的剪不斷理還亂的三角關(guān)系,樓主就著自己對(duì)它們的淺顯認(rèn)識(shí),來粗略地理理以備忘,有不對(duì)之處還望斧正。2016-07-07javascript類型系統(tǒng)——日期Date對(duì)象全面了解
下面小編就為大家?guī)硪黄猨avascript類型系統(tǒng)——日期Date對(duì)象全面了解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07淺談checkbox的一些操作(實(shí)戰(zhàn)經(jīng)驗(yàn))
checkbox看起來很簡(jiǎn)單,有時(shí)很頭疼,有什么難的,下面就為大家介紹下checkbox的一些操作,不了解的朋友不要錯(cuò)過2013-11-11注釋的藝術(shù)——JS里直接寫HTML,無需轉(zhuǎn)義
注釋的藝術(shù)——JS里直接寫HTML,無需轉(zhuǎn)義...2006-12-12