es6-symbol
ECMAScript6 Symbol polyfill
Limitations
- Underneath it uses real string property names which can easily be retrieved (however accidental collision with other property names is unlikely)
- As it needs custom
toString
behavior to work properly. Original Symbol.prototype.toString
couldn't be implemented as specified, still it's accessible as Symbol.prototoype.properToString
Usage
If you want to make sure your environment implements Symbol
, do:
require('es6-symbol/implement');
If you'd like to use native version when it exists and fallback to polyfill if it doesn't, but without implementing Symbol
on global scope, do:
var Symbol = require('es6-symbol');
If you strictly want to use polyfill even if native Symbol
exists (hard to find a good reason for that), do:
var Symbol = require('es6-symbol/polyfill');
API
Best is to refer to specification. Still if you want quick look, follow examples:
var Symbol = require('es6-symbol');
var symbol = Symbol('My custom symbol');
var x = {};
x[symbol] = 'foo';
console.log(x[symbol]); 'foo'
var iterator, result;
if (possiblyIterable[Symbol.iterator]) {
iterator = possiblyIterable[Symbol.iterator]();
result = iterator.next();
while(!result.done) {
console.log(result.value);
result = iterator.next();
}
}
Installation
NPM
In your project path:
$ npm install es6-symbol
Browser
You can easily bundle es6-symbol for browser with modules-webmake
Tests ![Build Status](https://travis-ci.org/medikoo/es6-symbol.png)
$ npm test