phantom_bind_polyfill.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /////////////////////////////////////
  2. // Taken from ariya/phantomjs#10522
  3. //
  4. Function.prototype.bind = function bind(that) { // .length is 1
  5. var target = this;
  6. if (typeof target != "function") {
  7. throw new TypeError("Function.prototype.bind called on incompatible " + target);
  8. }
  9. var args = Array.prototype.slice.call(arguments, 1); // for normal call
  10. var bound = function () {
  11. if (this instanceof bound) {
  12. var result = target.apply(
  13. this,
  14. args.concat(Array.prototype.slice.call(arguments))
  15. );
  16. if (Object(result) === result) {
  17. return result;
  18. }
  19. return this;
  20. } else {
  21. return target.apply(
  22. that,
  23. args.concat(Array.prototype.slice.call(arguments))
  24. );
  25. }
  26. };
  27. function Empty() {};
  28. if(target.prototype) {
  29. Empty.prototype = target.prototype;
  30. bound.prototype = new Empty();
  31. Empty.prototype = null;
  32. }
  33. return bound;
  34. };