-
-
Save MiguelCastillo/38005792d33373f4d08c to your computer and use it in GitHub Desktop.
/** | |
* 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))); | |
}; | |
}; | |
} |
It's more a visual thing than a function thing... I like how it reads.
Why do you concatenate an empty array with args
and Array.prototype.slice.call(arguments)
, instead of doing args.concat(Array.prototype.slice.call(arguments))
? Also, I feel like it might be more efficient/faster to do Array.prototype.slice.call(arguments)
once, and then get the args
from that.
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.
On this line, why do you put parentheses around the
fn
variable? I've seen that before, but always wondered what the purpose is.