javascript下的call函數

來源:互聯網
上載者:User

今天盧石給我看了個代碼,突然看到了一個call函數,不甚明白,也沒有找到對應的function call(),故想應當是JavaScript裡的函數,後來google一下。看了別人的例子:http://xiaofeizm55333.javaeye.com/blog/80913

明白了很多。

例如:method.call(node, args)其實就是相當於node.method(args). 其中method為一個function,也就是能實現某種功能的函數。

function add(a, b){

alert(a+b);

}

 

function sub(a,b){

alert(a-b);

}

 

add.call(sub, a, b);//the same with sub.add(a, b);because here the sub function is a object, also somebody call inheritance

另外就還有一個apply函數,是與call函數類似的。call(obj, args1, args2, args3,...),apply(obj,[args1, args2, args3,...])兩者的區別就在於obj後面的參數是單個的,還是數群組類型的。

就是這些了。

相關文章

聯繫我們

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