Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
es6-symbol
Advanced tools
The es6-symbol npm package provides a polyfill for the Symbol type introduced in ECMAScript 6. It offers a way to create unique identifiers that can be used as property keys in objects without the risk of name clashes. This is particularly useful for creating private or special properties that should not interfere with other properties in the object.
Creating symbols
This feature allows the creation of symbols, which can be used as unique identifiers. The code sample demonstrates how to create a symbol using the es6-symbol package.
var symbol = require('es6-symbol');
var mySymbol = symbol('mySymbol');
Using symbols as property keys
Symbols can be used as property keys in objects. This code sample shows how to assign and access a property in an object using a symbol as the key.
var symbol = require('es6-symbol');
var mySymbol = symbol('mySymbol');
var obj = {};
obj[mySymbol] = 'value';
console.log(obj[mySymbol]); // 'value'
Well-known symbols
The package provides access to well-known symbols defined by the ECMAScript specification, such as Symbol.iterator. This code sample demonstrates how to access a well-known symbol.
var symbol = require('es6-symbol');
var iteratorSymbol = symbol.iterator;
console.log(typeof iteratorSymbol); // 'symbol'
core-js is a modular standard library for JavaScript, which includes polyfills for ECMAScript up to 2021. It covers a broader range of ECMAScript features compared to es6-symbol, including symbols. core-js is more comprehensive but might be overkill if you only need symbol functionality.
babel-runtime provides a set of polyfills required for a full ES2015+ environment. It includes support for symbols among other features. Compared to es6-symbol, babel-runtime is part of the Babel ecosystem, making it a good choice if you're already using Babel for transpiling your JavaScript code.
For more information about symbols see following links
Underneath it uses real string property names which can easily be retrieved, however accidental collision with other property names is unlikely.
If you'd like to use native version when it exists and fallback to ponyfill if it doesn't, use es6-symbol as following:
var Symbol = require("es6-symbol");
If you want to make sure your environment implements Symbol
globally, do:
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:
var Symbol = require("es6-symbol/polyfill");
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");
// 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();
}
}
In your project path:
$ npm install es6-symbol
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
$ npm test
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
3.1.4 (2024-03-01)
Maintenance Improvements
FAQs
ECMAScript 6 Symbol polyfill
The npm package es6-symbol receives a total of 7,588,016 weekly downloads. As such, es6-symbol popularity was classified as popular.
We found that es6-symbol demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.