JS中call、apply的用法說明

來源:互聯網
上載者:User

標籤:style   blog   http   java   os   使用   io   strong   ar   

JS Call()與Apply()的區別

 

ECMAScript規範給所有函數都定義了Call()與apply()兩個方法,call與apply的第一個參數都是需要調用的函數對象,在函數體內這個參數就是this的值,剩餘的參數是需要傳遞給函數的值,call與apply的不同就是call傳的值可以是任意的,而apply傳的剩餘值必須為數組。

例如:function add(a, b) { return a + b; }

function sub(a, b) { return a - b; }

/*apply用法

* var a1 = sub.apply(add, [4, 2]);

*var a2= add.apply(sub, [4, 2]);  

*/

var a1 = sub.call(add, 4, 2);

var a2= add.call(sub, 4, 2);

輸出:a1=2  a2=6

 

感覺還是有意未盡,更有意思的還在下面

js總是認為他是萬能的,既然進階語言會繼承,我js也不能示弱:JS模仿繼承

  function fun1() {

            this.a = 123;

        this.add = function () { return  this.a }

        }

        function fun2() {

            this.a = 456;

        }

        var f1=new fun1()

        var f2=new fun2()

        var a = f1.add.call(f2);  // a輸出的是456

這裡就是把啊f1的方法拿給f2來使用,f2便可以使用f1中所有的方法,這不正是進階語言中繼承的概念嘍。當然根據綜上可擴充出多繼承,使用多個call便可以實現多繼承

   function fun1() {

 

            this.add = function () { return this.a }

        }

        function fun2() {

   

            this.sub = function () { return this.a-this.b }

        }

        function fun3() {

            this.a = 10;

            this.b = 2;

             fun1.call(this);

            fun2.call(this);

        }

        var f3 = new fun3()

        alert(f3.add());

        alert(f3.sub());如此,想要繼承誰就可以繼承誰,我js天下無敵 O(∩_∩)O哈哈~

 

 

 

1、方法定義

call方法:
文法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]])
定義:調用一個對象的一個方法,以另一個對象替換當前對象。
說明:
call 方法可以用來代替另一個對象調用一個方法。call 方法可將一個函數的物件內容從初始的上下文改變為由 thisObj 指定的新對象。
如果沒有提供 thisObj 參數,那麼 Global 對象被用作 thisObj。

apply方法:

文法:apply([thisObj[,argArray]])
定義:應用某一對象的一個方法,用另一個對象替換當前對象。
說明:
如果 argArray 不是一個有效數組或者不是 arguments 對象,那麼將導致一個 TypeError。
如果沒有提供 argArray 和 thisObj 任何一個參數,那麼 Global 對象將被用作 thisObj, 並且無法被傳遞任何參數。

 

2、常用執行個體

a、

Java代碼  
  1. function add(a,b)  
  2. {  
  3.     alert(a+b);  
  4. }  
  5. function sub(a,b)  
  6. {  
  7.     alert(a-b);  
  8. }  
  9.   
  10. add.call(sub,3,1);   
function add(a,b){    alert(a+b);}function sub(a,b){    alert(a-b);}add.call(sub,3,1); 

 這個例子中的意思就是用 add 來替換 sub,add.call(sub,3,1) == add(3,1) ,所以運行結果為:alert(4); // 注意:js 中的函數其實是對象,函數名是對 Function 對象的引用。

這裡其實還是調用的add(3, 1); 但是,在add這個函數執行的時候,當前的上下文this已經不是add對象的了,而是sub對象的。 

 

b、

Java代碼  
  1. function Animal(){    
  2.     this.name = "Animal";    
  3.     this.showName = function(){    
  4.         alert(this.name);    
  5.     }    
  6. }    
  7.   
  8. function Cat(){    
  9.     this.name = "Cat";    
  10. }    
  11.    
  12. var animal = new Animal();    
  13. var cat = new Cat();    
  14.     
  15. //通過call或apply方法,將原本屬於Animal對象的showName()方法交給對象cat來使用了。    
  16. //輸入結果為"Cat"    
  17. animal.showName.call(cat,",");    
  18. //animal.showName.apply(cat,[]);  
function Animal(){      this.name = "Animal";      this.showName = function(){          alert(this.name);      }  }  function Cat(){      this.name = "Cat";  }   var animal = new Animal();  var cat = new Cat();    //通過call或apply方法,將原本屬於Animal對象的showName()方法交給對象cat來使用了。  //輸入結果為"Cat"  animal.showName.call(cat,",");  //animal.showName.apply(cat,[]);

 call 的意思是把 animal 的方法放到cat上執行,原來cat是沒有showName() 方法,現在是把animal 的showName()方法放到 cat上來執行,所以this.name 應該是 Cat

 

c、實現繼承

Java代碼  
  1. function Animal(name){      
  2.     this.name = name;      
  3.     this.showName = function(){      
  4.         alert(this.name);      
  5.     }      
  6. }      
  7.     
  8. function Cat(name){    
  9.     Animal.call(this, name);    
  10. }      
  11.     
  12. var cat = new Cat("Black Cat");     
  13. cat.showName();  
 function Animal(name){         this.name = name;         this.showName = function(){             alert(this.name);         }     }        function Cat(name){       Animal.call(this, name);   }        var cat = new Cat("Black Cat");    cat.showName();

 Animal.call(this) 的意思就是使用 Animal對象代替this對象,那麼 Cat中不就有Animal的所有屬性和方法了嗎,Cat對象就能夠直接調用Animal的方法以及屬性了.

 

d、多重繼承

Java代碼  
  1. function Class10()  
  2. {  
  3.     this.showSub = function(a,b)  
  4.     {  
  5.         alert(a-b);  
  6.     }  
  7. }  
  8.   
  9. function Class11()  
  10. {  
  11.     this.showAdd = function(a,b)  
  12.     {  
  13.         alert(a+b);  
  14.     }  
  15. }  
  16.   
  17. function Class2()  
  18. {  
  19.     Class10.call(this);  
  20.     Class11.call(this);  
  21. }  
function Class10(){    this.showSub = function(a,b)    {        alert(a-b);    }}function Class11(){    this.showAdd = function(a,b)    {        alert(a+b);    }}function Class2(){    Class10.call(this);    Class11.call(this);}

 很簡單,使用兩個 call 就實現多重繼承了
當然,js的繼承還有其他方法,例如使用原型鏈,這個不屬於本文的範疇,只是在此說明call 的用法。說了call ,當然還有 apply,這兩個方法基本上是一個意思,區別在於 call 的第二個參數可以是任意類型,而apply的第二個參數必須是數組,也可以是arguments
還有 callee,caller..

JS中call、apply的用法說明

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.