Last active
May 28, 2023 12:20
-
-
Save MiguelCastillo/38005792d33373f4d08c to your computer and use it in GitHub Desktop.
Bind polyfill
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
/** | |
* Function bind polyfill | |
* https://github.com/ariya/phantomjs/issues/10522 | |
*/ | |
if (!Function.prototype.bind) { | |
Function.prototype.bind = function (context /* ...args */) { | |
var fn = this; | |
var args = Array.prototype.slice.call(arguments, 1); | |
if (typeof(fn) !== 'function') { | |
throw new TypeError('Function.prototype.bind - context must be a valid function'); | |
} | |
return function () { | |
return fn.apply(context, args.concat(Array.prototype.slice.call(arguments))); | |
}; | |
}; | |
} |
I could do args.concat(Array.prototype.slice.call(arguments))
. It's just what I typed... 😄
You mean to do Array.prototype.slice.call(arguments)
outside of the anonymous function I return? It's key concat both, the args from the call to bind and the args coming into the anonymous function. That's is because you need to handle code like fn.apply.bind
where the call to bind gets its own arguments and the callback also gets its own arguments. Or maybe I misunderstood your suggestion.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why do you concatenate an empty array with
args
andArray.prototype.slice.call(arguments)
, instead of doingargs.concat(Array.prototype.slice.call(arguments))
? Also, I feel like it might be more efficient/faster to doArray.prototype.slice.call(arguments)
once, and then get theargs
from that.