2013-03-14 75 views
12

我正在尋找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.

誰能更賜教了這一點。

+1

在不會使你的榜樣的差異,因爲'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

+0

我認爲DOM之間的主要區別方法'.call()'和'.apply()'是可以傳遞的參數的數量(一個只允許一個,我認爲,另一個更多)。我不確定對'.bind()'的引用是關於什麼的,但它通常指的是事件及其處理程序被「綁定」。 – 2013-03-14 08:40:22

回答

12

applycall是一樣的事情,除了一個接受傳遞給參數形式的另一個函數的參數。

bind做同樣的事情callapply取決於您使用但不調用該函數馬上相反,它與你的參數綁定到this當函數從一個新的名爲返回一個新的功能框架範圍或上下文,this仍然會保持你綁定的任何東西。綁定還可以防止您的構造函數被applycall「黑客攻擊」,因爲無論有人通過call還是或apply嘗試覆蓋this,它都將使用綁定參數this

下面是一個例子:

function Profile(u) { 
    this.user = u; 
    this.getUser = function() { 
     return this.user; 
    }; 
} 

function Profile2(u) { 
    this.user = u; 
    this.getUser = (function() { 
     return this.user; 
    }); 
} 

function Profile3(u) { 
    this.user = u; 
    this.getUser = (function() { 
     return this.user; 
    }); 
} 

var x = new Profile('guest'); 
var x2 = new Profile2('guest'); 
var x3 = new Profile3('guest'); 

alert(x.getUser.apply({ 
    user: 'Vinoth' 
})); // Vinoth 
alert(x2.getUser.call({ 
    user: 'Babu' 
})); // babu 
alert(x3.getUser.bind(x3).call({ 
    user: 'Nandan' 
})); // Guest 
3

bind創建具有相同功能的身體一個新的函數,然後返回新功能
call調用不同的傳遞的上下文相同的功能和參數都被明確寫入 apply在不同的通過調用相同的功能但參數必須以數組形式傳遞

var f = function(p1,p2){ var s = this; }

var newFunc = f.bind(window, 1, 2); 
//here newFunc is a function which when you will call will have this as window and p1 = 1 and p2 = 2 

f.call(window, 1 , 2); 
//by executing this line this = window p1 = 1 and p2 = 2 

f.call(document, 2, 3); 
//by executing this line this = document p1 = 2 and p2 = 3 

f.apply(window,[1,2]); 
//by executing this line this = window p1 = 1 and p2 = 2 
+0

'.bind'不執行該功能。 – 2013-03-14 08:43:56

+0

是的,sry .. ill編輯我的回答 – 2013-03-14 08:45:01

+1

另外,'this'不是上下文。它是由調用或綁定設置的局部變量。 – RobG 2013-03-14 08:51:43

2

簡單地說有應用之間沒有什麼不同()和()調用只有它們之間不同的是,你通過。在適用()你必須通過參數作爲一個數組參數,其中呼叫()您以逗號分隔的形式傳遞參數的方法。

談到綁定方法,這是在EcmaScript5中引入的新方法,尤其是用於在調用對象方法時解析this範圍。 this在異步方法調用中特別有用。

+1

'this'不是範圍,它(本質上)是由調用設置的局部變量。 – RobG 2013-03-14 08:49:40

相關問題