mirror of
https://github.com/nodejs/node.git
synced 2025-05-19 00:41:33 +00:00

PR-URL: https://github.com/nodejs/io.js/pull/2286 Reviewed-By: Roman Reiss <me@silverwind.io>
72 lines
2.1 KiB
Markdown
72 lines
2.1 KiB
Markdown
# es6-symbol
|
||
## ECMAScript 6 Symbol polyfill
|
||
|
||
For more information about symbols see following links
|
||
- [Symbols in ECMAScript 6 by Axel Rauschmayer](http://www.2ality.com/2014/12/es6-symbols.html)
|
||
- [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)
|
||
- [Specification](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-symbol-constructor)
|
||
|
||
### Limitations
|
||
|
||
Underneath it uses real string property names which can easily be retrieved, however accidental collision with other property names is unlikely.
|
||
|
||
### Usage
|
||
|
||
It’s safest to use *es6-symbol* as a [ponyfill](http://kikobeats.com/polyfill-ponyfill-and-prollyfill/) – a polyfill which doesn’t touch global objects:
|
||
|
||
```javascript
|
||
var Symbol = require('es6-symbol');
|
||
```
|
||
|
||
If you want to make sure your environment implements `Symbol` globally, do:
|
||
|
||
```javascript
|
||
require('es6-symbol/implement');
|
||
```
|
||
|
||
If you strictly want to use polyfill even if native `Symbol` exists (hard to find a good reason for that), do:
|
||
|
||
```javascript
|
||
var Symbol = require('es6-symbol/polyfill');
|
||
```
|
||
|
||
#### API
|
||
|
||
Best is to refer to [specification](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-symbol-objects). Still if you want quick look, follow examples:
|
||
|
||
```javascript
|
||
var Symbol = require('es6-symbol');
|
||
|
||
var symbol = 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
|
||
|
||
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: [Browserify](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/)
|
||
|
||
## Tests [](https://travis-ci.org/medikoo/es6-symbol)
|
||
|
||
$ npm test
|