Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ECMAScript 6 compatibility shims for legacy JS engines

License

NotificationsYou must be signed in to change notification settings

es-shims/es6-shim

 
 

Repository files navigation

Provides compatibility shims so that legacy JavaScript engines behave asclosely as possible to ECMAScript 6 (Harmony).

github actionscoverageLicenseDownloads

npm badge

HTML version of the final ECMAScript 6 spec

Installation

If you want to use it in browser:

  • Just includees6-shim before your scripts.
  • Includees5-shim especially if your browser doesn't support ECMAScript 5 - but every JS engine requires thees5-shim to correct broken implementations, so it's strongly recommended to always include it. Additionally,es5-shim should be loaded beforees6-shim.

Fornode.js,io.js, or anynpm-managed workflow (this is the recommended method):

npm install es6-shim

Alternative methods:

  • component install paulmillr/es6-shim if you’re usingcomponent(1).
  • bower install es6-shim if you’re usingBower.

In both browser and node you may also want to includeunorm; see theString.prototype.normalize section for details.

Safe shims

Math functions’ accuracy is 1e-11.

  • Reflect

  • Symbol (only if it already exists)

    • match (and correspondingString#match,String#startsWith,String#endsWith,String#includes,RegExp support)
    • replace (and correspondingString#replace support)
    • search (and correspondingString#search support)
    • split (and correspondingString#split support)

Well-known symbols will only be provided if the engine already hasSymbol support.

These methods are part of "Annex B", which means that although they are a defacto standard, you shouldn't use them. None the less, thees6-shim provides them and normalizes their behavior across browsers.

Subclassing

TheMap,Set, andPromise implementations are subclassable.You should use the following pattern to create a subclass in ES5 which will continue to work in ES6:

require('es6-shim');functionMyPromise(exec){varpromise=newPromise(exec);Object.setPrototypeOf(promise,MyPromise.prototype);// ...returnpromise;}Object.setPrototypeOf(MyPromise,Promise);MyPromise.prototype=Object.create(Promise.prototype,{constructor:{value:MyPromise}});

String.prototype.normalize

Including a proper shim forString.prototype.normalize would increase the size of this library by a factor of more than 4.So instead we recommend that you install theunorm package alongsidees6-shim if you needString.prototype.normalize.Seepaulmillr#134 for more discussion.

WeakMap shim

It is not possible to implement WeakMap in pure javascript.Thees6-collections implementation doesn't hold values strongly, which is critical for the collection.es6-shim decided to not include an incorrect shim.

WeakMap has very unusual use-cases, so you probably won't need it at all (use simpleMap instead).

Getting started

require('es6-shim');varassert=require('assert');assert.equal(true,'abc'.startsWith('a'));assert.equal(false,'abc'.endsWith('a'));assert.equal(true,'john alice'.includes('john'));assert.equal('123'.repeat(2),'123123');assert.equal(false,NaN===NaN);assert.equal(true,Object.is(NaN,NaN));assert.equal(true,-0===0);assert.equal(false,Object.is(-0,0));varresult=Object.assign({a:1},{b:2});assert.deepEqual(result,{a:1,b:2});assert.equal(true,isNaN('a'));assert.equal(false,Number.isNaN('a'));assert.equal(true,Number.isNaN(NaN));assert.equal(true,isFinite('123'));assert.equal(false,Number.isFinite('123'));assert.equal(false,Number.isFinite(Infinity));// Tests if value is a number, finite,// >= -9007199254740992 && <= 9007199254740992 and floor(value) === valueassert.equal(false,Number.isInteger(2.4));assert.equal(1,Math.sign(400));assert.equal(0,Math.sign(0));assert.equal(-1,Math.sign(-400));varfound=[5,10,15,10].find(function(item){returnitem/2===5;});assert.equal(10,found);varfoundIndex=[5,10,15,10].findIndex(function(item){returnitem/2===5;});assert.equal(1,foundIndex);// Replacement for `{}` key-value storage.// Keys can be anything.varmap=newMap([['Bob',42],['Foo','bar']]);map.set('John',25);map.set('Alice',400);map.set(['meh'],555);assert.equal(undefined,map.get(['meh']));// undefined because you need to use exactly the same object.map.delete('Alice');map.keys();map.values();assert.equal(4,map.size);// Useful for storing unique items.varset=newSet([0,1]);set.add(2);set.add(5);assert.equal(true,set.has(0));assert.equal(true,set.has(1));assert.equal(true,set.has(2));assert.equal(false,set.has(4));assert.equal(true,set.has(5));set.delete(5);assert.equal(false,set.has(5));// Promises, see// http://www.slideshare.net/domenicdenicola/callbacks-promises-and-coroutines-oh-my-the-evolution-of-asynchronicity-in-javascript// https://github.com/petkaantonov/bluebird/#what-are-promises-and-why-should-i-use-themPromise.resolve(5).then(function(value){assert.equal(value,5);if(value)thrownewError('whoops!');// do some stuffreturnanotherPromise();}).catch(function(e){assert.equal(e.message,'whoops!');assert.equal(true,einstanceofError);// any errors thrown asynchronously end up here});

Caveats

  • Object.setPrototypeOf /Reflect.setPrototypeOf
    • Note that null objects (Object.create(null), eg, an object withnull as its[[Prototype]]) can not have their[[Prototype]] changed except via a nativeObject.setPrototypeOf.
  • Well-knownSymbols
    • In order to make them work cross-realm, these are created with the globalSymbol registry viaSymbol.for. This does not violate the spec, but it does mean thatSymbol.for('Symbol.search') === Symbol.search will betrue, which it would not by default in a fresh compliant realm.

The project was initially based on es6-shim by Axel Rauschmayer.

About

ECMAScript 6 compatibility shims for legacy JS engines

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript98.3%
  • HTML1.7%

[8]ページ先頭

©2009-2025 Movatter.jp