Guest User

Untitled

a guest
Jan 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. delete Function.prototype.bind;
  2.  
  3. var nativeCall = Function.prototype.call, nativeApply = Function.prototype.apply, call,
  4. nativeArraySlice = Array.prototype.slice, nativeArrayConcat = Array.prototype.concat;
  5.  
  6. if (typeof Function.prototype.bind == 'function') {
  7. // This won't recurse because the native version of `Function#bind` doesn't depend on
  8. // `Function#call`.
  9. call = nativeCall.call(Function.prototype.bind, nativeCall, null);
  10. } else {
  11. call = function(method, context) {
  12. return nativeCall.call(nativeApply, method, context,
  13. nativeCall.call(nativeArraySlice, arguments, 2));
  14. };
  15. Function.prototype.bind = function (context) {
  16. var parameters = call(nativeArraySlice, arguments, 1), method = this;
  17. return function bound() {
  18. // See the problem here? `call` is used three times. This isn't a problem,
  19. // until you redefine `call` as a bound function.
  20. return call(nativeApply, method, context, call(nativeArrayConcat, parameters,
  21. call(nativeArraySlice, arguments, 0)));
  22. };
  23. };
  24. // This will create an infinite recusion when `Function#bind` is called because all
  25. // bound functions depend on `call` (see line 18), and you're redefining it as a
  26. // bound function.
  27. call = nativeCall.call(Function.prototype.bind, nativeCall, null);
  28. }
  29.  
  30. // This will recurse indefinitely...
  31. call();
  32.  
  33. // As will this...
  34. Function.prototype.bind();
  35.  
  36. // Look at the source code of `call`...you can see through simple inspection that the
  37. // function is attempting to recursively invoke itself.
  38. function bound() {
  39. return call(nativeApply, method, context, call(nativeArrayConcat, parameters,
  40. call(nativeArraySlice, arguments, 0)));
  41. }
  42.  
  43. // The solution is to remove line 27 and define `call` (and `apply`, in the case of
  44. // your original Gist) as a simple generic method (e.g., lines 11-14), rather than in
  45. // terms of `bind`.
Add Comment
Please, Sign In to add comment