Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- delete Function.prototype.bind;
- var nativeCall = Function.prototype.call, nativeApply = Function.prototype.apply, call,
- nativeArraySlice = Array.prototype.slice, nativeArrayConcat = Array.prototype.concat;
- if (typeof Function.prototype.bind == 'function') {
- // This won't recurse because the native version of `Function#bind` doesn't depend on
- // `Function#call`.
- call = nativeCall.call(Function.prototype.bind, nativeCall, null);
- } else {
- call = function(method, context) {
- return nativeCall.call(nativeApply, method, context,
- nativeCall.call(nativeArraySlice, arguments, 2));
- };
- Function.prototype.bind = function (context) {
- var parameters = call(nativeArraySlice, arguments, 1), method = this;
- return function bound() {
- // See the problem here? `call` is used three times. This isn't a problem,
- // until you redefine `call` as a bound function.
- return call(nativeApply, method, context, call(nativeArrayConcat, parameters,
- call(nativeArraySlice, arguments, 0)));
- };
- };
- // This will create an infinite recusion when `Function#bind` is called because all
- // bound functions depend on `call` (see line 18), and you're redefining it as a
- // bound function.
- call = nativeCall.call(Function.prototype.bind, nativeCall, null);
- }
- // This will recurse indefinitely...
- call();
- // As will this...
- Function.prototype.bind();
- // Look at the source code of `call`...you can see through simple inspection that the
- // function is attempting to recursively invoke itself.
- function bound() {
- return call(nativeApply, method, context, call(nativeArrayConcat, parameters,
- call(nativeArraySlice, arguments, 0)));
- }
- // The solution is to remove line 27 and define `call` (and `apply`, in the case of
- // your original Gist) as a simple generic method (e.g., lines 11-14), rather than in
- // terms of `bind`.
Add Comment
Please, Sign In to add comment