从需要的Underscore.js _.bindAll()函数的说明 [英] need explanation of the _.bindAll() function from Underscore.js

查看:113
本文介绍了从需要的Underscore.js _.bindAll()函数的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习一些Backbone.js的,我已经看到了很多,其中 _。bindAll()使用实例。我经历了整个Backbone.js的和underscore.js文档页面读取想方设法把它做什么的感觉,但我还是很为它做什么很模糊。这里是下划线的解释是:


  _。bindAll(对象,[*方法名])


  
  

绑定了一些关于方法
  对象,由指定的方法名,以
  在该对象的上下文中运行
  每当他们被调用。非常方便
  结合函数会
  用作事件处理程序,这
  否则将一个调用
  相当无用这一点。如果没有方法名
  被提供,该对象的所有的
  功能属性将被绑定到
  吧。

  VAR buttonView = {
  标签:下划线,
  的onClick:函数(){警报(点击:'+ this.label); },
  onHover:函数(){的console.log('徘徊:'+ this.label); }
};_.bindAll(buttonView);jQuery的('#underscore_button')绑定(点击,buttonView.onClick)。
= GT;当点击该按钮,this.label将有正确的值...


如果您可以通过给另一个例子或许或者一些口头解释帮助在这里,什么是AP preciated。我试图寻找更多的教程或例子,但无转起来服务于我所需要的。大多数人似乎只知道它会自动...


解决方案

\r
\r

VAR牛=功能(名称){\r
    this.name =名称;\r
}\r
Cow.prototype.moo =功能(){\r
    。的document.getElementById('输出')的innerHTML + = this.name +'穆斯'+'< BR>';\r
}\r
\r
VAR cow1 =新牛(爱丽丝);\r
VAR cow2 =新牛('鲍勃');\r
\r
cow1.moo(); //爱丽丝穆斯\r
cow2.moo(); //鲍勃·穆斯\r
\r
VAR FUNC = cow1.moo;\r
FUNC(); //不是你所期望的,因为函数调用这个窗口===\r
_.bindAll(cow1,'哞哞');\r
FUNC = cow1.moo;\r
FUNC(); //爱丽丝穆斯

\r

< D​​IV ID =输出/>\r
&LT;脚本src=\"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js\"></script>\r
\r
\r

不幸的是,实际的绑定所有功能只适用于功能右边的对象。要包括上原型定义你需要作为附加参数传递明确地定义这些函数名的函数 _。bindAll()

总之,你想要一个解释:基本上,它可以让你替换具有相同的名称和行为的函数的对象上的功能,而且还绑定到该对象,所以此== = theObject 即使没有调用它作为一种方法( theObject.method())。

I've been learning some backbone.js and I've seen plenty of instances where _.bindAll() is used. I have read through the entire backbone.js and underscore.js documentation page to try to get a sense of what it does, but I still am very fuzzy as to what it does. Here is underscore's explanation:

_.bindAll(object, [*methodNames]) 

Binds a number of methods on the object, specified by methodNames, to be run in the context of that object whenever they are invoked. Very handy for binding functions that are going to be used as event handlers, which would otherwise be invoked with a fairly useless this. If no methodNames are provided, all of the object's function properties will be bound to it.

var buttonView = {
  label   : 'underscore',
  onClick : function(){ alert('clicked: ' + this.label); },
  onHover : function(){ console.log('hovering: ' + this.label); }
};

_.bindAll(buttonView);

jQuery('#underscore_button').bind('click', buttonView.onClick);
=> When the button is clicked, this.label will have the correct value...

If you can help out here by giving another example perhaps or some verbal explanation, anything would be appreciated. I tried to search for more tutorials or examples, but nil turn up that serve what I needed. Most people seem to just know what it does automatically...

解决方案

var Cow = function(name) {
    this.name = name;
}
Cow.prototype.moo = function() {
    document.getElementById('output').innerHTML += this.name + ' moos' + '<br>';
}

var cow1 = new Cow('alice');
var cow2 = new Cow('bob');

cow1.moo(); // alice moos
cow2.moo(); // bob moos

var func = cow1.moo;
func(); // not what you expect since the function is called with this===window
_.bindAll(cow1, 'moo');
func = cow1.moo;
func(); // alice moos

<div id="output" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Unfortunately the actual "bind all" functionality only works on functions right on the object. To include a function that is defined on the prototype you need to pass those function names explicitely as additional arguments to _.bindAll().

Anyway, you wanted an explanation: Basically it allows you to replace a function on an object with a function that has the same name and behaviour, but is also bound to that object, so this === theObject even without calling it as a method (theObject.method()).

这篇关于从需要的Underscore.js _.bindAll()函数的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆