js的5種繼承方式——前端面試


js主要有以下幾種繼承方式:對象冒充,call()方法,apply()方法,原型鏈繼承以及混合方式。下面就每種方法就代碼講解具體的繼承是怎么實現的。

1、繼承第一種方式:對象冒充

 1 function Parent(username){
 2   this.username = username;
 3   this.hello = function(){
 4    alert(this.username);
 5   }
 6 }
 7 function Child(username,password){
 8   //通過以下3行實現將Parent的屬性和方法追加到Child中,從而實現繼承
 9   //第一步:this.method是作為一個臨時的屬性,並且指向Parent所指向的對象,
10   //第二步:執行this.method方法,即執行Parent所指向的對象函數
11   //第三步:銷毀this.method屬性,即此時Child就已經擁有了Parent的所有屬性和方法 
12   this.method = Parent; 13   this.method(username);//最關鍵的一行
14   delete this.method;
15 this.password = password; 16 this.world = function(){ 17 alert(this.password); 18 } 19 } 20 var parent = new Parent("zhangsan"); 21 var child = new Child("lisi","123456"); 22 parent.hello(); 23 child.hello(); 24 child.world();

2、繼承第二種方式:call()方法方式

call方法是Function類中的方法 
call方法的第一個參數的值賦值給類(即方法)中出現的this 
call方法的第二個參數開始依次賦值給類(即方法)所接受的參數

 1 function test(str){
 2   alert(this.name + " " + str);
 3 }
 4 var object = new Object();
 5 object.name = "zhangsan";
 6 test.call(object,"langsin");//此時,第一個參數值object傳遞給了test類(即方法)中出現的this,而第二個參數"langsin"則賦值給了test類(即方法)的str
 7 function Parent(username){
 8   this.username = username;
 9   this.hello = function(){
10    alert(this.username);
11   }
12 }
13 function Child(username,password){
14   Parent.call(this,username); 15   this.password = password;
16   this.world = function(){
17    alert(this.password);
18   }
19 }
20 var parent = new Parent("zhangsan");
21 var child = new Child("lisi","123456");
22 parent.hello();
23 child.hello();
24 child.world();
 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()方法方式

apply方法接受2個參數, 
A、第一個參數與call方法的第一個參數一樣,即賦值給類(即方法)中出現的this 
B、第二個參數為數組類型,這個數組中的每個元素依次賦值給類(即方法)所接受的參數

 1 function Parent(username){ 
 2   this.username = username; 
 3   this.hello = function(){ 
 4    alert(this.username); 
 5   } 
 6 } 
 7 function Child(username,password){ 
 8   Parent.apply(this,new Array(username)); 
 9   this.password = password; 
10   this.world = function(){ 
11    alert(this.password); 
12   } 
13 } 
14 var parent = new Parent("zhangsan"); 
15 var child = new Child("lisi","123456"); 
16 parent.hello(); 
17 child.hello(); 
18 child.world();
 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、繼承的第四種方式:原型鏈方式

實現原理是使子類原型對象指向父類的實例以實現繼承,即重寫類的原型,弊端是不能直接實現多繼承

即子類通過prototype將所有在父類中通過prototype追加的屬性和方法都追加到Child,從而實現了繼承

 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(); 
 1 function Person(){ 
 2 } 
 3 Person.prototype.hello = "hello"; 
 4 Person.prototype.sayHello = function(){ 
 5   alert(this.hello); 
 6 } 
 7 function Child(){ 
 8 } 
 9 Child.prototype = new Person();//這行的作用是:將Parent中將所有通過prototype追加的屬性和方法都追加到Child,從而實現了繼承 
10 Child.prototype.world = "world"; 
11 Child.prototype.sayWorld = function(){ 
12   alert(this.world); 
13 } 
14 var c = new Child(); 
15 c.sayHello(); 
16 c.sayWorld(); 

5、繼承的第五種方式:混合方式

混合了call方式、原型鏈方式

function Parent()  
{  
  
    this.sayAge=function()  
    {  
        console.log(this.age);  
    }  
}  
  
Parent.prototype.sayParent=function()  
{  
   alert("this is parentmethod!!!");  
}  
  
function Child(firstname)  
{  
    Parent.call(this);  
    this.fname=firstname;  
    this.age=40;  
    this.saySomeThing=function()  
    {  
        console.log(this.fname);  
        this.sayAge();  
    }  
}  
  
Child.prototype=new  Parent();  
var child=new Child("張");  
child.saySomeThing();  
child.sayParent();  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM