From a266e3c898e9ee1ced0f45199c92c08bd2ce8b27 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 20 Oct 2013 23:45:53 -0700 Subject: [PATCH] Ensure functions are rebound correctly by shallow cloning the `__bindData__` before passing it to `createBound`. [closes #371] --- lodash.js | 1 + test/test.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lodash.js b/lodash.js index 60abd2d6f..3857f433b 100644 --- a/lodash.js +++ b/lodash.js @@ -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; } diff --git a/test/test.js b/test/test.js index 1f10f1922..dfab68ea8 100644 --- a/test/test.js +++ b/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']); + }); }()); /*--------------------------------------------------------------------------*/