js實現繼承的5種方式

來源:互聯網
上載者:User

標籤:語言   prototype   script   實現繼承   ted   沒有   tle   let   src   

   js是門靈活的語言,實現一種功能往往有多種做法,ECMAScript沒有明確的繼承機制,而是通過模仿實現的,根據js語言的本身的特性,js實現繼承有以下通用的幾種方式
1.使用對象冒充實現繼承(該種實現方式可以實現多繼承)
實現原理:讓父類的建構函式成為子類的方法,然後調用該子類的方法,通過this關鍵字給所有的屬性和方法賦值

Js代碼  
  1. function Parent(firstname)  
  2. {  
  3.     this.fname=firstname;  
  4.     this.age=40;  
  5.     this.sayAge=function()  
  6.     {  
  7.         console.log(this.age);  
  8.     }  
  9. }  
  10. function Child(firstname)  
  11. {  
  12.     this.parent=Parent;  
  13.     this.parent(firstname);  
  14.     delete this.parent;  
  15.     this.saySomeThing=function()  
  16.     {  
  17.         console.log(this.fname);  
  18.         this.sayAge();  
  19.     }  
  20. }  
  21. var mychild=new  Child("李");  
  22. mychild.saySomeThing();  

 

2.採用call方法改變函數上下文實現繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則採用5混合模式)
實現原理:改變函數內部的函數上下文this,使它指向傳入函數的具體對象

Js代碼  
  1. function Parent(firstname)  
  2. {  
  3.     this.fname=firstname;  
  4.     this.age=40;  
  5.     this.sayAge=function()  
  6.     {  
  7.         console.log(this.age);  
  8.     }  
  9. }  
  10. function Child(firstname)  
  11. {  
  12.   
  13.     this.saySomeThing=function()  
  14.     {  
  15.         console.log(this.fname);  
  16.         this.sayAge();  
  17.     }  
  18.    this.getName=function()  
  19.    {  
  20.        return firstname;  
  21.    }  
  22.   
  23. }  
  24. var child=new Child("張");  
  25. Parent.call(child,child.getName());  
  26. child.saySomeThing();  

 

3.採用Apply方法改變函數上下文實現繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則採用5混合模式)
實現原理:改變函數內部的函數上下文this,使它指向傳入函數的具體對象

Js代碼  
  1. function Parent(firstname)  
  2. {  
  3.     this.fname=firstname;  
  4.     this.age=40;  
  5.     this.sayAge=function()  
  6.     {  
  7.         console.log(this.age);  
  8.     }  
  9. }  
  10. function Child(firstname)  
  11. {  
  12.   
  13.     this.saySomeThing=function()  
  14.     {  
  15.         console.log(this.fname);  
  16.         this.sayAge();  
  17.     }  
  18.     this.getName=function()  
  19.     {  
  20.         return firstname;  
  21.     }  
  22.   
  23. }  
  24. var child=new Child("張");  
  25. Parent.apply(child,[child.getName()]);  
  26. child.saySomeThing();  

 

4.採用原型鏈的方式實現繼承
實現原理:使子類原型對象指向父類的執行個體以實現繼承,即重寫類的原型,弊端是不能直接實現多繼承

Js代碼  
  1. function Parent()  
  2. {  
  3.   
  4.     this.sayAge=function()  
  5.     {  
  6.         console.log(this.age);  
  7.     }  
  8. }  
  9. function Child(firstname)  
  10. {  
  11.     this.fname=firstname;  
  12.     this.age=40;  
  13.     this.saySomeThing=function()  
  14.     {  
  15.         console.log(this.fname);  
  16.         this.sayAge();  
  17.     }  
  18. }  
  19.   
  20. Child.prototype=new  Parent();  
  21. var child=new Child("張");  
  22. child.saySomeThing();  

 

5.採用混合模式實現繼承

Js代碼  
    1. function Parent()  
    2. {  
    3.   
    4.     this.sayAge=function()  
    5.     {  
    6.         console.log(this.age);  
    7.     }  
    8. }  
    9.   
    10. Parent.prototype.sayParent=function()  
    11. {  
    12.    alert("this is parentmethod!!!");  
    13. }  
    14.   
    15. function Child(firstname)  
    16. {  
    17.     Parent.call(this);  
    18.     this.fname=firstname;  
    19.     this.age=40;  
    20.     this.saySomeThing=function()  
    21.     {  
    22.         console.log(this.fname);  
    23.         this.sayAge();  
    24.     }  
    25. }  
    26.   
    27. Child.prototype=new  Parent();  
    28. var child=new Child("張");  
    29. child.saySomeThing();  
    30. child.sayParent(); 

js實現繼承的5種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.