5d0f12a5f8a0ae226b47646cbf179ed8e9a5f74e23d566a33996486b8e86af40ffa363e66fc853989417cfff6743eab7a3b888ef36f9726a681cae8ec00824 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # globalThis <sup>[![Version Badge][npm-version-svg]][npm-url]</sup>
  2. [![github actions][actions-image]][actions-url]
  3. [![coverage][codecov-image]][codecov-url]
  4. [![dependency status][deps-svg]][deps-url]
  5. [![dev dependency status][dev-deps-svg]][dev-deps-url]
  6. [![License][license-image]][license-url]
  7. [![Downloads][downloads-image]][downloads-url]
  8. [![npm badge][npm-badge-png]][npm-url]
  9. An ECMAScript spec-compliant polyfill/shim for `globalThis`. Invoke its "shim" method to shim `globalThis` if it is unavailable.
  10. This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the [spec proposal](https://github.com/tc39/proposal-global).
  11. Most common usage:
  12. ```js
  13. var globalThis = require('globalthis')(); // returns native globalThis if compliant
  14. /* or */
  15. var globalThis = require('globalthis/polyfill')(); // returns native globalThis if compliant
  16. ```
  17. ## Example
  18. ```js
  19. var assert = require('assert');
  20. // the below function is not CSP-compliant, but reliably gets the
  21. // global object in sloppy mode in every engine.
  22. var getGlobal = Function('return this');
  23. assert.equal(globalThis, getGlobal());
  24. ```
  25. ```js
  26. /* when `globalThis` is not present */
  27. var shimmedGlobal = require('globalthis').shim();
  28. /* or */
  29. var shimmedGlobal = require('globalthis/shim')();
  30. assert.equal(shimmedGlobal, globalThis);
  31. assert.equal(shimmedGlobal, getGlobal());
  32. ```
  33. ```js
  34. /* when `globalThis` is present */
  35. var shimmedGlobal = require('globalthis').shim();
  36. assert.equal(shimmedGlobal, globalThis);
  37. assert.equal(shimmedGlobal, getGlobal());
  38. ```
  39. ## Tests
  40. Simply clone the repo, `npm install`, and run `npm test`
  41. [npm-url]: https://npmjs.org/package/globalthis
  42. [npm-version-svg]: https://versionbadg.es/ljharb/globalThis.svg
  43. [deps-svg]: https://david-dm.org/ljharb/globalThis.svg?theme=shields.io
  44. [deps-url]: https://david-dm.org/ljharb/globalThis
  45. [dev-deps-svg]: https://david-dm.org/ljharb/globalThis/dev-status.svg?theme=shields.io
  46. [dev-deps-url]: https://david-dm.org/ljharb/globalThis#info=devDependencies
  47. [npm-badge-png]: https://nodei.co/npm/globalthis.png?downloads=true&stars=true
  48. [license-image]: https://img.shields.io/npm/l/globalthis.svg
  49. [license-url]: LICENSE
  50. [downloads-image]: https://img.shields.io/npm/dm/globalthis.svg
  51. [downloads-url]: https://npm-stat.com/charts.html?package=globalthis
  52. [codecov-image]: https://codecov.io/gh/es-shims/globalThis/branch/main/graphs/badge.svg
  53. [codecov-url]: https://app.codecov.io/gh/es-shims/globalThis/
  54. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/globalThis
  55. [actions-url]: https://github.com/es-shims/globalThis/actions