难道骨干做_.bindAll默认了吧? [英] Does backbone do _.bindAll by default now?
问题描述
本教程表明,我们需要做的 _.bindAll
来获得的这个
在我们的职能正确的值。看来, _。bindAll
不再与骨干必需的。下面code登录同样的事情两次:
This tutorial suggests that we need to do _.bindAll
to get the correct value of this
in our functions. It seems that _.bindAll
is no longer required with Backbone. The following code logs the same thing twice:
var TestView = Backbone.View.extend({
initialize: function () { _.bindAll(this, 'func1'); },
func1: function () { console.log(this); },
func2: function () { console.log(this); }
});
var testView = new TestView();
testView.func1();
testView.func2();
我在假设更正 bindAll
不再是必需的,还是我只是让一个愚蠢的错误?
Am I correct in assuming that bindAll
is no longer required, or am I just making a stupid mistake?
推荐答案
这仍然是必要的,当方法被调用出来的类的上下文。由于在上下文中you'recalling它,它是不是一个错误,你有没有需要它。
It is still necessary when the method is called out of context of the Class. Since you'recalling it in context, it isn't a mistake that you haven't needed it.
由于下划线文件中的 _。bindAll
(自提<一个href=\"http://documentcloud.github.com/underscore/#bindAll\">http://documentcloud.github.com/underscore/#bindAll),它是非常方便的绑定将要用作事件处理函数,否则就会与调用一个相当无用的这个
。你也将使用它的方法在哪里您需要创建一个回调。
As mentioned in the underscore documentation for _.bindAll
(http://documentcloud.github.com/underscore/#bindAll), it's "very handy for binding functions that are going to be used as event handlers, which would otherwise be invoked with a fairly useless this
." You would also use it for methods where you need to create a callback.
要了解如何有回调的差异,看看这个小提琴。 http://jsfiddle.net/joshvermaire/YQdZu/
To see how there are differences for callbacks, look at this fiddle. http://jsfiddle.net/joshvermaire/YQdZu/
这篇关于难道骨干做_.bindAll默认了吧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!