ES6 Shim

0.35.8 · maintenance · verified Sun Apr 19

The `es6-shim` package provides compatibility shims to enable ECMAScript 6 (Harmony) features in legacy JavaScript environments that lack native support. This includes core language features like `Promise`, `Map`, and `Set`, as well as numerous methods on `String`, `Number`, and `Array` prototypes such as `String.prototype.includes` or `Number.isInteger`. It operates by patching the global scope, making these features available as if they were natively implemented. The current stable version is 0.35.8, which primarily focuses on bug fixes, robustness improvements, and spec compliance. As a polyfill library, its release cadence is driven by bug reports and updates to the ECMAScript specification, rather than frequent feature additions, differentiating it from libraries introducing new paradigms. It aims to make older engines behave as closely as possible to the ES6 standard.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to include `es6-shim` and `es5-shim` and then use various ECMAScript 6 features like `Map`, `Promise`, `String.prototype.includes`, and `Number.isInteger` that are polyfilled by the library.

// In a browser environment, you would include these via script tags in your HTML:
// <script src="node_modules/es5-shim/es5-shim.js"></script>
// <script src="node_modules/es6-shim/es6-shim.js"></script>

// For Node.js or a bundler:
require('es5-shim'); // Recommended for comprehensive polyfilling
require('es6-shim');

// Now, ES6 features are available globally
console.log('--- Map Example ---');
const myMap = new Map();
myMap.set('key1', 'value1');
myMap.set('key2', 'value2');
console.log('Map size:', myMap.size); 
console.log('Has key1:', myMap.has('key1')); 

console.log('\n--- Promise Example ---');
new Promise((resolve) => {
  setTimeout(() => resolve('Promise resolved!'), 100);
}).then(message => {
  console.log(message); 
}).catch(error => {
  console.error('Promise rejected:', error);
});

console.log('\n--- String.prototype.includes Example ---');
const greeting = 'Hello world!';
console.log('"world" included:', greeting.includes('world')); 
console.log('"foo" included:', greeting.includes('foo'));     

console.log('\n--- Number.isInteger Example ---');
console.log('Is 42 an integer:', Number.isInteger(42));     
console.log('Is 3.14 an integer:', Number.isInteger(3.14)); 

view raw JSON →