es6-symbol

ECMAScript6 Symbol polyfill

npm install es6-symbol
2 downloads in the last week
4 downloads in the last month

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.prototoype.toString couldn't be implemented as specified, still it's accessible as Symbol.prototoype.origigalToString

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 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 = new Symbol('My custom symbol');
var x = {};

x[symbol] = 'foo';
console.log(x[symbol]); 'foo'

// Detect iterable:
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

$ npm test
npm loves you