Skip to content

Commit 66bb646

Browse files
committed
Merge pull request #415 from HSSC/master
use _.isFunction in _.invoke
2 parents e58af52 + 2206092 commit 66bb646

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

test/collections.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ $(document).ready(function() {
180180
equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
181181
});
182182

183+
// Relevant when using ClojureScript
184+
test('collections: invoke when strings have a call method', function() {
185+
String.prototype.call = function(){return 42;}
186+
var list = [[5, 1, 7], [3, 2, 1]];
187+
var s = "foo";
188+
equals(s.call(), 42, "call function exists");
189+
var result = _.invoke(list, 'sort');
190+
equals(result[0].join(', '), '1, 5, 7', 'first array sorted');
191+
equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
192+
delete String.prototype.call;
193+
equals(s.call, undefined, "call function removed");
194+
});
195+
183196
test('collections: pluck', function() {
184197
var people = [{name : 'moe', age : 30}, {name : 'curly', age : 50}];
185198
equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'pulls names out of objects');

underscore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
_.invoke = function(obj, method) {
219219
var args = slice.call(arguments, 2);
220220
return _.map(obj, function(value) {
221-
return (method.call ? method || value : value[method]).apply(value, args);
221+
return (_.isFunction(method) ? method || value : value[method]).apply(value, args);
222222
});
223223
};
224224

0 commit comments

Comments
 (0)