js 中的繼承

面試的時候老是被問到js的繼承,平時都是應用,最近有時間就把js 的繼承整理了一下,和java 中的繼承作了一下比較,代碼以下:
javascript

js繼承有5種實現方式:java

一、對象冒充
<script>
 
function Parent(username){ 
    this.username = username; 
    this.hello = function(){ 
      alert(this.username); 
    } 
  } 
  function Child(username,password){ 
    //經過如下3行實現將Parent的屬性和方法追加到Child中,從而實現繼承 
    //第一步:this.method是做爲一個臨時的屬性,而且指向Parent所指向的對象, 
    //第二步:執行this.method方法,即執行Parent所指向的對象函數 
    //第三步:銷燬this.method屬性,即此時Child就已經擁有了Parent的全部屬性和方法 
    this.method = Parent; 
    this.method(username);//最關鍵的一行 
    delete this.method; 

    this.password = password; 
    this.world = function(){ 
      alert(this.password); 
    } 
  } 
  var parent = new Parent("zhangsan"); 
  var child = new Child("lisi","123456"); 
  parent.hello(); 
  child.hello(); 
  child.world(); 
</script>

 

二、繼承第二種方式:call()方法方式
<script>
  /*
  call方法是Function類中的方法 
  call方法的第一個參數的值賦值給類(即方法)中出現的this 
  call方法的第二個參數開始依次賦值給類(即方法)所接受的參數
  */

  function test(str){ 
    alert(this.name + " " + str); 
  } 
  var object = new Object(); 
  object.name = "zhangsan"; 
  test.call(object,"langsin");//此時,第一個參數值object傳遞給了test類(即方法)中出現的this,而第二個參數"langsin"則賦值給了test類(即方法)的str 

  function Parent(username){ 
    this.username = username; 
    this.hello = function(){ 
      alert(this.username); 
    } 
  } 
  function Child(username,password){ 
    Parent.call(this,username); 
    
    this.password = password; 
    this.world = function(){ 
      alert(this.password); 
    } 
  } 
  var parent = new Parent("zhangsan"); 
  var child = new Child("lisi","123456"); 
  parent.hello(); 
  child.hello(); 
  child.world(); 
</script>

 

三、繼承的第三種方式:apply()方法方式
<script>
  /*apply方法接受2個參數, 
    A、第一個參數與call方法的第一個參數同樣,即賦值給類(即方法)中出現的this 
    B、第二個參數爲數組類型,這個數組中的每一個元素依次賦值給類(即方法)所接受的參數 
  */
  function Parent(username){ 
    this.username = username; 
    this.hello = function(){ 
      alert(this.username); 
    } 
  } 
  function Child(username,password){ 
    Parent.apply(this,new Array(username)); 
    
    this.password = password; 
    this.world = function(){ 
      alert(this.password); 
    } 
  } 
  var parent = new Parent("zhangsan"); 
  var child = new Child("lisi","123456"); 
  parent.hello(); 
  child.hello(); 
  child.world(); 
  </script>
四、原型方式繼承
<script type="text/javascript">  
    function Person(name,age){  
        this.name=name;  
        this.age=age;  
    }  
    Person.prototype.sayHello=function(){  
        alert("使用原型獲得Name:"+this.name);  
    }  
    var per=new Person("zhangsan",21);  
    per.sayHello(); //輸出:使用原型獲得Name:zhangsan  

      
    function Student(){}  
    Student.prototype=new Person("lisi",21);  
    var stu=new Student();  
    Student.prototype.grade=5;  
    Student.prototype.intr=function(){  
        alert(this.grade);  
    }  
    stu.sayHello();//輸出:使用原型獲得Name:lisi  
    stu.intr();//輸出:5  
</script>

  五、繼承的第五種方式:混合方式 
  混合了call方式、原型鏈方式面試

 function Parent(hello){ 
    this.hello = hello; 
  } 
  Parent.prototype.sayHello = function(){ 
    alert(this.hello); 
  } 

  function Child(hello,world){ 
    Parent.call(this,hello);//將父類的屬性繼承過來 
    this.world = world;//新增一些屬性 
  } 

  Child.prototype = new Parent();//將父類的方法繼承過來 

  Child.prototype.sayWorld = function(){//新增一些方法 
    alert(this.world); 
  } 

  var c = new Child("zhangsan","lisi"); 
  c.sayHello(); 
  c.sayWorld();

  今天到此爲止吧數組

相關文章
相關標籤/搜索