Skip to content

Commit

Permalink
Finish the previous _.bind revert.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Dec 4, 2012
1 parent 35796f1 commit 16a229a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ $(document).ready(function() {
var newBoundf = new Boundf();
equal(newBoundf.hello, undefined, "function should not be bound to the context, to comply with ECMAScript 5");
equal(Boundf().hello, "moe curly", "When called without the new operator, it's OK to be bound to the context");
ok(newBoundf instanceof F, "a bound instance is an instance of the original function");
});

test("bindAll", function() {
Expand Down
9 changes: 6 additions & 3 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,16 +573,19 @@
// optionally). Binding with arguments is also known as `curry`.
// Delegates to **ECMAScript 5**'s native `Function.bind` if available.
// We check for `func.bind` first, to fail fast when `func` is undefined.
_.bind = function bind(func, context) {
_.bind = function(func, context) {
var args, bound;
if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
if (!_.isFunction(func)) throw new TypeError;
args = slice.call(arguments, 2);
return bound = function() {
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
var result = func.apply(this, args.concat(slice.call(arguments)));
ctor.prototype = func.prototype;
var self = new ctor;
ctor.prototype = null;
var result = func.apply(self, args.concat(slice.call(arguments)));
if (Object(result) === result) return result;
return this;
return self;
};
};

Expand Down

0 comments on commit 16a229a

Please sign in to comment.