1. globalthis
ECMAScript spec-compliant polyfill/shim for `globalThis`
globalthis
Package: globalthis
Created by: ljharb
Last modified: Tue, 30 Apr 2024 02:35:59 GMT
Version: 1.0.4
License: MIT
Downloads: 94,487,433
Repository: https://github.com/ljharb/System.global

Install

npm install globalthis
yarn add globalthis

globalThis Version Badge

github actions
coverage
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

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code