ECMAScript 5 Compatibility Shims

4.6.7 · maintenance · verified Sun Apr 19

es5-shim and es5-sham are JavaScript libraries designed to provide ECMAScript 5 (ES5) compatibility shims and polyfills for legacy JavaScript engines that lack native support for these features. The current stable version is 4.6.7, which was last published over four years ago (as of early 2022), indicating an extremely slow release cadence and placing the project firmly in maintenance mode, rather than active development. es5-shim.js faithfully emulates many ES5 methods like Array iteration methods, Date.now, and Function.prototype.bind, making them available in older environments. es5-sham.js, on the other hand, provides best-effort shims for features that cannot be fully or faithfully emulated (such as Object.create with property descriptors, Object.freeze, or Object.defineProperty). A key differentiator is its direct monkey-patching of the global JavaScript context, making it suitable for environments without modern transpilation or module bundling, particularly for ensuring compatibility in very old browser environments like Internet Explorer 8.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load both es5-shim and es5-sham via `require` and verifies that core ES5 features like `Array.isArray`, `Object.keys`, and `Function.prototype.bind` are available and functional, even in environments that might not natively support them.

const isArraySupported = typeof Array.isArray === 'function';
console.log(`Array.isArray supported before shim: ${isArraySupported}`);

// Load the es5-shim to polyfill standard ES5 features
require('es5-shim/es5-shim');

// Load the es5-sham for best-effort shims of unfaithful ES5 features
require('es5-shim/es5-sham');

const isArraySupportedAfterShim = typeof Array.isArray === 'function';
console.log(`Array.isArray supported after shim: ${isArraySupportedAfterShim}`);

const arr = [1, 2, 3];
const obj = { a: 1 };

console.log(`Array.isArray([1,2,3]): ${Array.isArray(arr)}`);
console.log(`Array.isArray({a:1}): ${Array.isArray(obj)}`);

// Demonstrate an Object.keys shim
const keys = Object.keys(obj);
console.log(`Object.keys({a:1}): ${JSON.stringify(keys)}`);

// Demonstrate Function.prototype.bind
const unboundFn = function(a, b) { return this.value + a + b; };
const boundFn = unboundFn.bind({ value: 10 }, 5);
console.log(`Bound function result: ${boundFn(2)}`);

view raw JSON →