mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 07:47:49 +00:00
Ensure functions are rebound correctly by shallow cloning the __bindData__ before passing it to createBound. [closes #371]
This commit is contained in:
@@ -1588,6 +1588,7 @@
|
||||
}
|
||||
var bindData = func && func.__bindData__;
|
||||
if (bindData && bindData !== true) {
|
||||
bindData = nativeSlice.call(bindData);
|
||||
if (isBind && !(bindData[1] & 1)) {
|
||||
bindData[4] = thisArg;
|
||||
}
|
||||
|
||||
20
test/test.js
20
test/test.js
@@ -436,6 +436,26 @@
|
||||
test('should throw a TypeError if `func` is not a function', 1, function() {
|
||||
raises(function() { _.bind(); }, TypeError);
|
||||
});
|
||||
|
||||
test('should rebind functions correctly', 3, function() {
|
||||
function func() {
|
||||
var args = [this];
|
||||
push.apply(args, arguments);
|
||||
return args;
|
||||
}
|
||||
|
||||
var object1 = {},
|
||||
object2 = {},
|
||||
object3 = {};
|
||||
|
||||
var bound1 = _.bind(func, object1),
|
||||
bound2 = _.bind(bound1, object2, 'a'),
|
||||
bound3 = _.bind(bound1, object3, 'b');
|
||||
|
||||
deepEqual(bound1(), [object1]);
|
||||
deepEqual(bound2(), [object1, 'a']);
|
||||
deepEqual(bound3(), [object1, 'b']);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user