Skip to content
ECMAScript spec-compliant polyfill/shim for `globalThis`.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows [actions] add automatic rebasing / merge commit blocking Dec 15, 2019
test [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `browserify`, `t… Dec 15, 2019
.eslintignore [Tests] remove `jscs` Jun 5, 2019
.eslintrc [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `browserify`, `t… Dec 15, 2019
.gitignore Only apps should have lockfiles Jul 26, 2018
.npmignore Dotfiles. Mar 30, 2016
.npmrc [meta] add `auto-changelog` Dec 15, 2019
.travis.yml
CHANGELOG.md
LICENSE Initial commit Mar 30, 2016
README.md readme: Fix casing + phrasing Apr 18, 2019
auto.js [New] add `auto` entry point Jul 26, 2018
browserShim.js implementation Mar 30, 2016
implementation.browser.js [Refactor] only use `global` in node; only check browser globals in b… Dec 15, 2019
implementation.js [Refactor] only use `global` in node; only check browser globals in b… Dec 15, 2019
index.js implementation Mar 30, 2016
package.json
polyfill.js Rename `System.global` to `global` Sep 27, 2016
shim.js [breaking] update property name, rename repo Jul 26, 2018

README.md

globalThis Version Badge

Build Status dependency status dev dependency status License Downloads

npm badge

An ECMAScript spec-compliant polyfill/shim for globalThis. Invoke its "shim" method to shim globalThis if it is unavailable.

This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the spec proposal.

Most common usage:

var globalThis = require('globalthis')(); // returns native globalThis if compliant
	/* or */
var globalThis = require('globalthis/polyfill')(); // returns native globalThis if compliant

Example

var assert = require('assert');

// the below function is not CSP-compliant, but reliably gets the
// global object in sloppy mode in every engine.
var getGlobal = Function('return this');

assert.equal(globalThis, getGlobal());
/* when `globalThis` is not present */
var shimmedGlobal = require('globalthis').shim();
	/* or */
var shimmedGlobal = require('globalthis/shim')();

assert.equal(shimmedGlobal, globalThis);
assert.equal(shimmedGlobal, getGlobal());
/* when `globalThis` is present */
var shimmedGlobal = require('globalthis').shim();

assert.equal(shimmedGlobal, globalThis);
assert.equal(shimmedGlobal, getGlobal());

Tests

Simply clone the repo, npm install, and run npm test

You can’t perform that action at this time.