自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JavaScript深入之繼承的多種方式和優(yōu)缺點(diǎn)

開發(fā) 前端
本文講解JavaScript各種繼承方式和優(yōu)缺點(diǎn)。我們一起來(lái)看一下吧。

寫在前面

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

但是注意:

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

1、原型鏈繼承 

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

問題:

1、引用類型的屬性被所有實(shí)例共享,舉個(gè)例子: 

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

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

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

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

優(yōu)點(diǎn):

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

2、可以在 Child 中向 Parent 傳參

舉個(gè)例子: 

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

缺點(diǎn):

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

3、組合繼承

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

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

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

4、原型式繼承 

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

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

缺點(diǎn):

包含引用類型的屬性值始終都會(huì)共享相應(yīng)的值,這點(diǎn)跟原型鏈繼承一樣。 

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

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

5、寄生式繼承

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

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

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

6、寄生組合式繼承

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

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

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

一次是設(shè)置子類型實(shí)例的原型的時(shí)候: 

  1. Child.prototype = new Parent(); 

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

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

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

  1. Parent.call(this, name); 

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

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

那么我們?cè)撊绾尉媲缶?,避免這一次重復(fù)調(diào)用呢?

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

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

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

最后我們封裝一下這個(gè)繼承方法: 

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

引用《JavaScript高級(jí)程序設(shè)計(jì)》中對(duì)寄生組合式繼承的夸贊就是:

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

 

責(zé)任編輯:龐桂玉 來(lái)源: 前端教程
相關(guān)推薦

2017-05-11 21:01:20

JavaScript創(chuàng)建對(duì)象面向?qū)ο缶幊?/a>

2016-12-27 09:10:29

JavaScript原型鏈繼承

2009-04-13 16:37:33

JSPWeb標(biāo)簽

2017-10-23 13:20:37

2023-10-08 08:46:29

Java遍歷方式

2025-01-21 09:10:00

2025-02-27 00:37:06

2011-03-10 14:19:56

JavaScript

2016-09-08 14:50:59

AndroidiPhoneiOS

2018-02-06 09:40:25

PythonOOP繼承

2021-09-13 10:27:34

云備份云恢復(fù)云計(jì)算

2010-09-17 10:07:28

2009-09-01 10:00:55

Tomcat集群方式

2010-06-19 14:19:18

RIP路由協(xié)議

2011-08-24 13:56:27

JavaScript

2019-10-22 10:48:48

Redis集群架構(gòu)

2021-10-09 09:52:17

云存儲(chǔ)公共云遷移

2017-06-26 10:35:58

前端JavaScript繼承方式

2023-07-06 16:08:52

物聯(lián)網(wǎng)人工智能

2009-09-04 08:33:25

VirtualBox虛
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)