Ensure functions are rebound correctly by shallow cloning the __bindData__ before passing it to createBound. [closes #371]

This commit is contained in:
John-David Dalton
2013-10-20 23:45:53 -07:00
parent f9c34d6808
commit a266e3c898
2 changed files with 21 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -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']);
});
}());
/*--------------------------------------------------------------------------*/