-
-
Save michaelficarra/503393 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.