Skip to content

Commit

Permalink
Fixes #2004 - Fix Function:bind when the resulting function is used a…
Browse files Browse the repository at this point in the history
…s constructor
  • Loading branch information
Arian authored and cpojer committed Aug 12, 2011
1 parent b1543a5 commit 1e24187
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions Source/Types/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,23 @@ Function.implement({
},

/*<!ES5>*/
bind: function(bind){
bind: function(that){
var self = this,
args = (arguments.length > 1) ? Array.slice(arguments, 1) : null;

return function(){
if (!args && !arguments.length) return self.call(bind);
if (args && arguments.length) return self.apply(bind, args.concat(Array.from(arguments)));
return self.apply(bind, args || arguments);
args = arguments.length > 1 ? Array.slice(arguments, 1) : null,
F = function(){};

var bound = function(){
var context = that, length = arguments.length;
if (this instanceof bound){
F.prototype = self.prototype;
context = new F;
}
var result = (!args && !length)

This comment has been minimized.

Copy link
@jdalton

jdalton Aug 13, 2011

when this instanceof bound the return should allow for the constructor returning a custom object

// yall know constructors can do this
function Foo() {
  return [1,2,3]
}

new Foo; // [1,2,3]
? self.call(context)
: self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments);
return context == that ? result : context;
};

This comment has been minimized.

Copy link
@jdalton

jdalton Aug 13, 2011

bound needs a custom .call/.apply method to auto curry bound args and to avoid issues with bound.call(new bound, ...) and bound.apply(new bound, ...)

return bound;
},
/*</!ES5>*/

Expand Down

0 comments on commit 1e24187

Please sign in to comment.