Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Forked from evilpie/gist:503343
Created August 1, 2010 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelficarra/503393 to your computer and use it in GitHub Desktop.
Save michaelficarra/503393 to your computer and use it in GitHub Desktop.
unless Function::bind?
Function::bind = (scope, args...) ->
target = this
if typeof target isnt "function" then throw new TypeError
bound = ->
unless this intanceof bound
return target.apply scope, [args..., arguments...]
F = ->
F.prototype = target.prototype
self = new F
result = target.apply self, [args..., arguments...]
return result if result is Object result
self
bound
if(Function.prototype.bind == null) {
Function.prototype.bind = function bind(scope) {
var target = this;
if (typeof target != "function") throw new TypeError();
var args = [].slice.call(arguments, 1);
var bound = function () {
if (!(this instanceof bound)) {
return target.apply(scope, [].concat.call(args, [].slice.call(arguments)));
}
var F = function(){};
F.prototype = target.prototype;
var self = new F;
var result = target.apply(self, [].concat.call(args, [].slice.call(arguments)));
if (Object(result) === result)
return result;
return self;
};
return bound;
};
}
Copy link

ghost commented Sep 7, 2011

You can remove the result isnt null condition on line 12 of the CoffeeScript version, and result !== null on line 14 of the JavaScript version, unless it's for performance reasons.

@michaelficarra
Copy link
Author

It was pretty much just a direct port from the es5-shim version, so I left it. It probably was for performance. I'll remove it anyway. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment