下面还列出了代码:
window.MyView = Backbone.View.extend({
ticks: 0,
initialize: function() {
//window.setInterval(this.onTimerTick, 1000); // arghhh.. can't understand the 'this' scoping
window.setInterval(this.onTimerTick2, 1000); // Works great with globals
},
render: function() {
this.$el.text(this.ticks);
},
onTimerTick: function() { // Trouble with this
this.ticks++;
this.render();
},
onTimerTick2: function() { // Using globals
window.ticks2++;
$('#count').text(window.ticks2);
}
});
window.My = new MyView({ el: $("#count") });
window.ticks2 = 0;
看代码,你看到我想使用onTimerTick函数,但是因为我不知道如何从window-this到My-this,我必须使用onTimerTick2中看到的方法。(通常我会使用 that=this,但在这种情况下还不够)
感谢您让我理解这一点(!)
谢谢
拉尔西