我正在尋找stackoverflow和網絡,無法得到適當的結果或解釋選址這三種方法之間的差異。綁定,應用和調用方法之間的區別?
據我瞭解,他們都做同樣的執行function/method in different context.
var google = {
makeBeer : function(arg1,arg2){
alert([arg1, arg2]);
}
}
google.makeBeer('water','soda');
這是我在谷歌對象的正常功能。現在,當我在這裏使用call和bind方法時,這裏是輸出。
var google = {
makeBeer: function (arg1, arg2) {
alert([arg1, arg2]);
}
}
google.makeBeer('water', 'soda');
function yahoo() {}
var yah = new yahoo();
google.makeBeer.call(yah, 'pepsi', 'coke');
function msn() {
}
var msn = new msn();
google.makeBeer.call(msn, 'sprite', 'limca');
我還沒有看到這樣做的目的,我可以反超並調用google.makeBeer three times with different arguments.
誰能更賜教了這一點。
在不會使你的榜樣的差異,因爲'google.makeBeer'不使用'this'。當被調用爲'google.makeBeer(...);'時,函數內部的this將引用'google'。當被稱爲'google.makeBeer.call(yah,...);'時,''這個'將指向'yah'。 'bind'實際上並不執行一個函數,它會創建一個新的函數,其中'this'和可選的一些參數綁定到傳遞的參數。請參閱https://developer.mozilla。org/en-US/docs/JavaScript/Reference/Operators/this和https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind – 2013-03-14 08:39:11
我認爲DOM之間的主要區別方法'.call()'和'.apply()'是可以傳遞的參數的數量(一個只允許一個,我認爲,另一個更多)。我不確定對'.bind()'的引用是關於什麼的,但它通常指的是事件及其處理程序被「綁定」。 – 2013-03-14 08:40:22