我正在学习backbone.js,对此感到困惑:我正在关注教程: http ://arturadib.com/hello-backbonejs/
正如您在第一个示例(1.js)中看到的那样:
(function($){
var ListView = Backbone.View.extend({
el: $('body'), // attaches `this.el` to an existing element.
initialize: function(){
_.bindAll(this, 'render'); // fixes loss of context for 'this' within methods
this.render(); // not all views are self-rendering. This one is.
},
render: function(){
$(this.el).append("<ul> <li>hello world</li> </ul>");
}
});
var listView = new ListView();
})(jQuery);
但是,如果我注释掉这句话:_.bindAll(this, 'render');
,这仍然有效。我在谷歌搜索过,有人说该方法bindAll()
是必要的,因为如果我切换上下文,调用this.render
可能不可用。我对“上下文”感到困惑。this.render
当调用 ( ) 将不可用时,也有人可以解释我吗?