這是代碼片段,這讓我困惑:的JavaScript綁定問題
var timer =
{
start: function()
{
var self = this;
/*Why the code below doesn't write to this:
window.setInterval(self.tick, 1000).*/
//Instead, it embedded into a function like this:
window.setInterval(function(){self.tick();}, 1000)
},
tick: function()
{
console.log("tick!");
}
}
timer.start();
爲什麼稍後沒有人會將其添加到'tick'而不是? – icktoofay 2013-04-07 03:22:04
因爲也許'tick'可以在其他地方使用。 – 2013-04-07 03:26:05