Symbol-ES6
Provides support for ES6
Symbol API in ES5
for older JS environments i.e. older browsers or NodeJS.
ES6 Symbol polyfill in pure ES5.
Install
NPM
Install it from npm
and require
it before any other modules:
$ npm install --save symbol-es6
;
CDN
If you prefer CDN, then just insert it into your HTML page on the top of other scripts:
Examples
"use strict"; var ES6 = ; console; //false var sym = Symbol;var sym2 = Symbol"bar"; console; //trueconsole; //falseconsole; //barconsole; //undefined console; //[ 'H', 'e', 'l', 'l', 'o' ] var it = 1 2; console; //{ done: false, value: [ 0, 1 ] }console; //{ done: false, value: [ 1, 2 ] }console; //{ done: true, value: undefined } it = 1 2; console; //{ done: false, value: 0 }console; //{ done: false, value: 1 }console; //{ done: true, value: undefined } {}console; //[object Object] BarprototypeSymboltoStringTag = "Bar"; console; //[object Bar] it = "Bar"Symboliterator; console; //{ done: false, value: 'B' }console; //{ done: false, value: 'a' }console; //{ done: false, value: 'r' } console; //fasleconsole; ///true { }console; //falseObject;console; //true ES6;//1//2 console; //[ 'B', 'a', 'r' ] { return Arrayprototypereduce;} console; //15 { thisname = name; thisroll = roll;} console; //Ariyan
Polyfills
-
Symbol
for()
keyFor
@@hasInstance
@@isConcatSpreadable
@@iterator
@@toStringTag
Symbol.prototype.toString()
Symbol.prototype.valueOf()
-
Function
Function.prototype[@@hasInstance]()
-
Array
Array.prototype.concat()
(ES6 version, addition of@@isConcatSpreadable
support)Array.prototype[@@iterator]()
Array.from()
Array.prototype.entries()
Array.prototype.keys()
-
Object
Object.prototype.toString()
(ES6 version, addition of@@toStringTag
support)
-
String
String.prototype[@@iterator]()
Limitation
Some ES6
features can't be implemented in ES5
natively like spread operator
, for..of
loop,
ES6
version of instanceOf
operator etc. So this module exports a object named ES6
globally,
that provides some approximate equivalent implementation of those features.
ES6
Object
This object provides,
isSymbol()
(It can be used as equivalent API of:typeof symbol === 'symbol'
)instanceOf()
(Provides ES6 version ofinstanceOf
)forOf()
(This method behaves exactly same as ES6for...of
loop)spreadOperator
(Gives same functionality of thespread operator
of ES6)
Contributing
Your PRs and stars are always welcome.
Please, try to follow:
- Clone the repository.
- Checkout
develop
branch. - Install dependencies.
- Add your new features or fixes.
- Build the project.
$ git clone https://github.com/rousan/symbol-es6.git$ cd symbol-es6$ git checkout develop$ npm i$ npm run build