Skip to content

Commit

Permalink
Merge pull request jashkenas#2141 from jridgewell/bindall-array
Browse files Browse the repository at this point in the history
Allow _.bindAll to take arrays
  • Loading branch information
jashkenas committed May 22, 2015
2 parents 92a3007 + 3cd0487 commit 5c3470a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 4 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@

var sayLast = moe.sayLast;
equal(sayLast(1, 2, 3, 4, 5, 6, 7, 'Tom'), 'hi: moe', 'createCallback works with any number of arguments');

_.bindAll(moe, ['getName']);
var getName = moe.getName;
equal(getName(), 'name: moe', 'flattens arguments into a single list');
});

test('memoize', function() {
Expand Down
9 changes: 6 additions & 3 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,13 @@
// are the method names to be bound. Useful for ensuring that all callbacks
// defined on an object belong to it.
_.bindAll = restArgs(function(obj, keys) {
if (keys.length < 1) throw new Error('bindAll must be passed function names');
return _.each(keys, function(key) {
keys = flatten(keys, false, false);
var index = keys.length;
if (index < 1) throw new Error('bindAll must be passed function names');
while (index--) {
var key = keys[index];
obj[key] = _.bind(obj[key], obj);
});
};
});

// Memoize an expensive function by storing its results.
Expand Down

0 comments on commit 5c3470a

Please sign in to comment.