What is es6-shim?
The es6-shim package is designed to provide compatibility shims so that legacy JavaScript engines behave as closely as possible to ECMAScript 6 (ES6). It includes shims for new methods and functions introduced in ES6, allowing developers to use these features in environments that do not yet support them natively.
What are es6-shim's main functionalities?
Array.prototype methods
Adds new methods to Array.prototype like find, findIndex, etc., enabling more expressive operations on arrays.
[1, 2, 3].find(x => x == 2)
String.prototype methods
Introduces new String methods such as startsWith, endsWith, and includes for easier string manipulation.
'hello'.startsWith('hell')
Number properties
Provides new static properties and methods on the Number object like isNaN and isFinite for improved number checking.
Number.isNaN(NaN)
Promises
Implements Promises for asynchronous programming, following the ES6 specification.
new Promise(function(resolve, reject) { resolve(42); })
Maps and Sets
Adds support for new collection types such as Map and Set, offering more options for data storage and manipulation.
new Map([[1, 'one'], [2, 'two']])
Other packages similar to es6-shim
core-js
A modular standard library for JavaScript, including polyfills for ECMAScript up to 2021. Offers broader coverage than es6-shim, including proposals not yet standardized.
babel-polyfill
Part of Babel, this package includes a custom regenerator runtime and core-js. It's more comprehensive but has been deprecated in favor of directly including core-js and the regenerator-runtime.
es5-shim
Focuses on ensuring ECMAScript 5 compatibility, providing shims for older JavaScript engines. It's more focused on ES5, making it complementary rather than directly comparable to es6-shim.
ES6 Shim
Provides compatibility shims so that legacy JavaScript engines behave as
closely as possible to ECMAScript 6 (Harmony).
Installation
If you want to use it in browser:
- Just include es6-shim before your scripts.
- Include es5-shim if your browser doesn't support ECMAScript 5.
component install paulmillr/es6-shim
if you’re using component(1).bower install es6-shim
if you’re using Twitter Bower.
For node.js:
npm install es6-shim
In both browser and node you may also want to include unorm
; see the
String.prototype.normalize
section for
details.
Safe shims
Map
, Set
(requires ES5)Promise
String
:
String.prototype
:
Number
:
Array
:
Array.prototype
:
Object
:
Math
:
acosh()
asinh()
atanh()
cbrt()
clz32()
cosh()
expm1()
hypot()
log2()
log10()
log1p()
sign()
sinh()
tanh()
trunc()
imul()
fround()
Math functions accuracy is 1e-11.
Subclassing
The Map
, Set
, and Promise
implementations are subclassable.
You should use the following pattern to create a subclass in ES5 which
will continue to work in ES6:
function MyPromise(exec) {
Promise.call(this, exec);
}
Object.setPrototypeOf(MyPromise, Promise);
MyPromise.prototype = Object.create(Promise.prototype, {
constructor: { value: MyPromise }
});
String.prototype.normalize
Including a proper shim for String.prototype.normalize
would
increase the size of this library by a factor of more than 4.
So instead we recommend that you install the
unorm
package alongside es6-shim
if you need String.prototype.normalize
.
See https://github.com/paulmillr/es6-shim/issues/134 for more
discussion.
WeakMap shim
It is not possible to implement WeakMap in pure javascript.
The es6-collections
implementation doesn't hold values strongly, which is critical
for the collection. es6-shim decided to not include an incorrect shim.
WeakMap has a very unusual use-case so you probably won't need it at all
(use simple Map
instead).
Getting started
'abc'.startsWith('a')
'abc'.endsWith('a')
'john alice'.includes('john')
'123'.repeat(2)
Object.is(NaN, NaN)
Object.assign({a: 1}, {b: 2})
Number.isNaN('123')
Number.isFinite('asd')
Number.isInteger(2.4)
Math.sign(400)
[5, 10, 15, 10].find(function (item) {return item / 2 === 5;})
[5, 10, 15, 10].findIndex(function (item) {return item / 2 === 5;})
var map = new Map()
map.set('John', 25)
map.set('Alice', 400)
map.set(['meh'], 555)
map.get(['meh'])
map.delete('Alice')
map.keys()
map.values()
map.size
var set = new Set()
set.add(1)
set.add(5)
set.has(1)
set.has(4)
set.delete(5)
Promise.resolve(5).then(function (value) {
if ( ... ) throw new Error("whoops!");
return anotherPromise();
}).catch(function (e) {
});
Other stuff:
License
The project was initially based on es6-shim by Axel Rauschmayer.
Current maintainers are: Paul Miller, Jordan Harband, and C. Scott Ananian.
The MIT License (MIT)
Copyright (c) 2013-2014 Paul Miller (http://paulmillr.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.