zoukankan      html  css  js  c++  java
  • 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();  
  • 相关阅读:
    ORACLE定时备份
    解压cpio.gz
    Fusioncharts的数字格式化
    linux apache+php+mysql安装及乱码解决办法
    两个Beta函数类型的积分及其一般形式
    【转载】巴塞尔问题(Basel Problem)的多种解法
    两本关于各种刁钻积分计算的书
    一个超几何函数类型的积分
    一个取整函数积分的一般形式
    Clausen Functions (and related series, functions, integrals)
  • 原文地址:https://www.cnblogs.com/greatluoluo/p/6273787.html
Copyright © 2011-2022 走看看