日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JavaScript深入之繼承的多種方式和優(yōu)缺點

寫在前面

創(chuàng)新互聯(lián)是一家專業(yè)提供長樂企業(yè)網(wǎng)站建設,專注與做網(wǎng)站、網(wǎng)站設計、H5頁面制作、小程序制作等業(yè)務。10年已為長樂眾多企業(yè)、政府機構(gòu)等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡公司優(yōu)惠進行中。

本文講解JavaScript各種繼承方式和優(yōu)缺點。

但是注意:

這篇文章更像是筆記,哎,再讓我感嘆一句:《JavaScript高級程序設計》寫得真是太好了!

1、原型鏈繼承

 
 
 
 
  1. function Parent () { 
  2.     this.name = 'kevin'; 
  3. Parent.prototype.getName = function () { 
  4.     console.log(this.name); 
  5. function Child () { 
  6. Child.prototype = new Parent();  
  7. var child1 = new Child(); 
  8. console.log(child1.getName()) // kevin

問題:

1、引用類型的屬性被所有實例共享,舉個例子:

 
 
 
 
  1. function Parent () { 
  2.     this.names = ['kevin', 'daisy']; 
  3. function Child () { 
  4. Child.prototype = new Parent(); 
  5. var child1 = new Child(); 
  6. child1.names.push('yayu'); 
  7. console.log(child1.names); // ["kevin", "daisy", "yayu"] 
  8. var child2 = new Child(); 
  9. console.log(child2.names); // ["kevin", "daisy", "yayu"]

2、在創(chuàng)建 Child 的實例時,不能向Parent傳參

2、借用構(gòu)造函數(shù)(經(jīng)典繼承)

 
 
 
 
  1. function Parent () { 
  2.     this.names = ['kevin', 'daisy']; 
  3. function Child () { 
  4.     Parent.call(this); 
  5. var child1 = new Child(); 
  6. child1.names.push('yayu'); 
  7. console.log(child1.names); // ["kevin", "daisy", "yayu"] 
  8. var child2 = new Child(); 
  9. console.log(child2.names); // ["kevin", "daisy"]

優(yōu)點:

1、避免了引用類型的屬性被所有實例共享

2、可以在 Child 中向 Parent 傳參

舉個例子:

 
 
 
 
  1. function Parent (name) { 
  2.     this.name = name; 
  3. function Child (name) { 
  4.     Parent.call(this, name); 
  5. var child1 = new Child('kevin'); 
  6. console.log(child1.name); // kevin 
  7. var child2 = new Child('daisy'); 
  8. console.log(child2.name); // daisy

缺點:

方法都在構(gòu)造函數(shù)中定義,每次創(chuàng)建實例都會創(chuàng)建一遍方法。

3、組合繼承

原型鏈繼承和經(jīng)典繼承雙劍合璧。

 
 
 
 
  1. function Parent (name) { 
  2.     this.name = name; 
  3.     this.colors = ['red', 'blue', 'green']; 
  4. Parent.prototype.getName = function () { 
  5.     console.log(this.name) 
  6. function Child (name, age) { 
  7.     Parent.call(this, name);   
  8.     this.age = age; 
  9. Child.prototype = new Parent(); 
  10. ChildChild.prototype.constructor = Child; 
  11. var child1 = new Child('kevin', '18'); 
  12. child1.colors.push('black'); 
  13. console.log(child1.name); // kevin 
  14. console.log(child1.age); // 18 
  15. console.log(child1.colors); // ["red", "blue", "green", "black"] 
  16. var child2 = new Child('daisy', '20'); 
  17. console.log(child2.name); // daisy 
  18. console.log(child2.age); // 20 
  19. console.log(child2.colors); // ["red", "blue", "green"]

優(yōu)點:融合原型鏈繼承和構(gòu)造函數(shù)的優(yōu)點,是 JavaScript 中最常用的繼承模式。

4、原型式繼承

 
 
 
 
  1. function createObj(o) { 
  2.     function F(){} 
  3.     F.prototype = o; 
  4.     return new F(); 
  5. }

就是 ES5 Object.create 的模擬實現(xiàn),將傳入的對象作為創(chuàng)建的對象的原型。

缺點:

包含引用類型的屬性值始終都會共享相應的值,這點跟原型鏈繼承一樣。

 
 
 
 
  1. var person = { 
  2.     name: 'kevin', 
  3.     friends: ['daisy', 'kelly'] 
  4. var person1 = createObj(person); 
  5. var person2 = createObj(person); 
  6. person1.name = 'person1'; 
  7. console.log(person2.name); // kevin 
  8. person1.firends.push('taylor'); 
  9. console.log(person2.friends); // ["daisy", "kelly", "taylor"]

注意:修改person1.name的值,person2.name的值并未發(fā)生改變,并不是因為person1和person2有獨立的 name 值,而是因為person1.name = 'person1',給person1添加了 name 值,并非修改了原型上的 name 值。

5、寄生式繼承

創(chuàng)建一個僅用于封裝繼承過程的函數(shù),該函數(shù)在內(nèi)部以某種形式來做增強對象,最后返回對象。

 
 
 
 
  1. function createObj (o) { 
  2.     var clone = Object.create(o); 
  3.     clone.sayName = function () { 
  4.         console.log('hi'); 
  5.     } 
  6.     return clone; 
  7. }

缺點:跟借用構(gòu)造函數(shù)模式一樣,每次創(chuàng)建對象都會創(chuàng)建一遍方法。

6、寄生組合式繼承

為了方便大家閱讀,在這里重復一下組合繼承的代碼:

 
 
 
 
  1. function Parent (name) { 
  2.     this.name = name; 
  3.     this.colors = ['red', 'blue', 'green']; 
  4. Parent.prototype.getName = function () { 
  5.     console.log(this.name) 
  6. function Child (name, age) { 
  7.     Parent.call(this, name); 
  8.     this.age = age; 
  9. Child.prototype = new Parent(); 
  10. var child1 = new Child('kevin', '18'); 
  11. console.log(child1)

組合繼承最大的缺點是會調(diào)用兩次父構(gòu)造函數(shù)。

一次是設置子類型實例的原型的時候:

 
 
 
 
  1. Child.prototype = new Parent();

一次在創(chuàng)建子類型實例的時候:

 
 
 
 
  1. var child1 = new Child('kevin', '18');

回想下 new 的模擬實現(xiàn),其實在這句中,我們會執(zhí)行:

 
 
 
 
  1. Parent.call(this, name);

在這里,我們又會調(diào)用了一次 Parent 構(gòu)造函數(shù)。

所以,在這個例子中,如果我們打印 child1 對象,我們會發(fā)現(xiàn) Child.prototype 和 child1 都有一個屬性為colors,屬性值為['red', 'blue', 'green']。

那么我們該如何精益求精,避免這一次重復調(diào)用呢?

如果我們不使用 Child.prototype = new Parent() ,而是間接的讓 Child.prototype 訪問到 Parent.prototype 呢?

看看如何實現(xiàn):

 
 
 
 
  1. function Parent (name) { 
  2.     this.name = name; 
  3.     this.colors = ['red', 'blue', 'green']; 
  4. Parent.prototype.getName = function () { 
  5.     console.log(this.name) 
  6. function Child (name, age) { 
  7.     Parent.call(this, name); 
  8.     this.age = age; 
  9. // 關鍵的三步 
  10. var F = function () {}; 
  11. F.prototype = Parent.prototype; 
  12. Child.prototype = new F(); 
  13. var child1 = new Child('kevin', '18'); 
  14. console.log(child1);

最后我們封裝一下這個繼承方法:

 
 
 
 
  1. function object(o) { 
  2.     function F() {} 
  3.     F.prototype = o; 
  4.     return new F(); 
  5. function prototype(child, parent) { 
  6.     var prototype = object(parent.prototype); 
  7.     prototype.constructor = child; 
  8.     child.prototype = prototype; 
  9. // 當我們使用的時候: 
  10. prototype(Child, Parent);

引用《JavaScript高級程序設計》中對寄生組合式繼承的夸贊就是:

這種方式的高效率體現(xiàn)它只調(diào)用了一次 Parent 構(gòu)造函數(shù),并且因此避免了在 Parent.prototype 上面創(chuàng)建不必要的、多余的屬性。與此同時,原型鏈還能保持不變;因此,還能夠正常使用 instanceof 和 isPrototypeOf。開發(fā)人員普遍認為寄生組合式繼承是引用類型最理想的繼承范式。


分享文章:JavaScript深入之繼承的多種方式和優(yōu)缺點
URL鏈接:http://www.5511xx.com/article/ccshoid.html