0

underscore文档中 bindAll 方法执行以下操作:

_.bindAll(object, *methodNames) // syntax

var buttonView = {
  label  : 'underscore',
  onClick: function(){ alert('clicked: ' + this.label); },
  onHover: function(){ console.log('hovering: ' + this.label); }
};
_.bindAll(buttonView, 'onClick', 'onHover');
// When the button is clicked, this.label will have the correct value.
jQuery('#underscore_button').bind('click', buttonView.onClick);

现在 bindAll() 方法绑定对象上的许多方法,由 methodNames 指定,以便在调用它们时在该对象的上下文中运行。现在真的有必要在上面的例子中调用 bindAll() 吗?

我认为调用buttonView.onClick会自动绑定this关键字buttonView不是吗?

4

0 回答 0