Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need explanation of the _.bindAll() function from Underscore.js

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...

like image 300
Nik So Avatar asked Jul 05 '11 11:07

Nik So


People also ask

What is _ each in JavaScript?

The _.each function accepts an array or an object, an iteratee function and an optional context object, the iteratee function is invoked once and in order for each array item The iteratee function provides 3 arguments item - The current iterated object (or value if an object was passed) i - The index of the iterated ...

Why underscore is used in JavaScript?

The underscore is simply a valid character in an identifier, so the method's name is _render . The method Foo can then only be called from within the class which defined it. In JavaScript you can't do this, so it's a typical design pattern to prefix the method with _ to show that it should be treated as private.

What is the use of underscore flip function?

The _. flip() method returns a function that works identically to given function but accepts the arguments in reverse order.

How do I run an underscore in JavaScript?

Adding Underscore to a Node. Once added, underscore can be referred in any of the Node. js modules using the CommonJS syntax: var _ = require('underscore'); Now we can use the object underscore (_) to operate on objects, arrays and functions.


1 Answers

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()).

like image 83
ThiefMaster Avatar answered Sep 19 '22 21:09

ThiefMaster