From da9758c2f3e478ab311be61378647a89f44a22b4 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 30 Nov 2012 23:16:39 -0800 Subject: [PATCH] =?UTF-8?q?Ensure=20`bound`=20result=20of=20`=5F.bind(func?= =?UTF-8?q?,=20=E2=80=A6)`=20is=20an=20instance=20of=20`bound`=20and=20`fu?= =?UTF-8?q?nc`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: d8176ad5eb45a3d675617676fc1eee4d9cbd6ebc --- lodash.js | 16 ++++++++-------- test/test.js | 8 ++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lodash.js b/lodash.js index 8348ccd5e..e90d3cccd 100644 --- a/lodash.js +++ b/lodash.js @@ -609,19 +609,19 @@ : partialArgs; } if (this instanceof bound) { - // get `func` instance if `bound` is invoked in a `new` expression - noop.prototype = func.prototype; - thisBinding = new noop; - // mimic the constructor's `return` behavior // http://es5.github.com/#x13.2.2 - var result = func.apply(thisBinding, args); - return isObject(result) - ? result - : thisBinding + var result = func.apply(this, args); + return isObject(result) ? result : this; } return func.apply(thisBinding, args); } + if (func && func.prototype) { + // ensure `new bound` is an instance of `bound` and `func` + noop.prototype = func.prototype; + bound.prototype = new noop + noop.prototype = null; + } return bound; } diff --git a/test/test.js b/test/test.js index 23445e144..d752f1224 100644 --- a/test/test.js +++ b/test/test.js @@ -209,6 +209,14 @@ bound(['b'], 'c'); deepEqual(args, ['a', ['b'], 'c']); }); + + test('ensure `new bound` is an instance of `bound` and `func`', function() { + var func = function() {}, + bound = _.bind(func, {}), + instance = new bound; + + ok(instance instanceof bound && instance instanceof func); + }); }()); /*--------------------------------------------------------------------------*/