我正在学习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);
Run Code Online (Sandbox Code Playgroud)
但是,如果我评论出这句话:_.bindAll(this, 'render');
这仍然有效.我在谷歌搜索过,有人说这个方法bindAll()
是必要的,因为如果我切换我的上下文,调用this.render
可能不可用.我对"背景"感到困惑.当call(this.render
)不可用时,还有人可以解释一下吗?