implementation.js 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. var naiveFallback = function () {
  2. if (typeof self === "object" && self) return self;
  3. if (typeof window === "object" && window) return window;
  4. throw new Error("Unable to resolve global `this`");
  5. };
  6. module.exports = (function () {
  7. if (this) return this;
  8. // Unexpected strict mode (may happen if e.g. bundled into ESM module)
  9. // Thanks @mathiasbynens -> https://mathiasbynens.be/notes/globalthis
  10. // In all ES5+ engines global object inherits from Object.prototype
  11. // (if you approached one that doesn't please report)
  12. try {
  13. Object.defineProperty(Object.prototype, "__global__", {
  14. get: function () { return this; },
  15. configurable: true
  16. });
  17. } catch (error) {
  18. // Unfortunate case of Object.prototype being sealed (via preventExtensions, seal or freeze)
  19. return naiveFallback();
  20. }
  21. try {
  22. // Safari case (window.__global__ is resolved with global context, but __global__ does not)
  23. if (!__global__) return naiveFallback();
  24. return __global__;
  25. } finally {
  26. delete Object.prototype.__global__;
  27. }
  28. })();