es-shims/get-own-property-symbols

Name: get-own-property-symbols

Owner: ECMAScript Shims

Description: ES6 Object.getOwnPropertySymbols partial polyfill

Created: 2015-04-16 11:18:06.0

Updated: 2018-05-24 14:05:43.0

Pushed: 2016-12-28 18:37:11.0

Homepage: null

Size: 69

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

get-own-property-symbols

build status

This is a widely compatible, Mobile friendly, and zero dependencies polyfill for Object.getOwnPropertySymbols.

getOwnPropertySymbols = require('get-own-property-symbols');

o = {};
s = Symbol();

 = 123;

ct.getOwnPropertyNames(o);  // []
wnPropertySymbols(o);       // [s]

ame as
ct.getOwnPropertySymbols(o);// [s]

This module brings in a global Symbol initializer too, together with Symbol.for and Symbol.keyFor methods.

s = Symbol.for('me');
ol.for('me') === s; // true

ol.keyFor(s); // 'me'

Common symbols like iterator are also defined including the Array.prototype[Symbol.iterator] and the String.prototype[Symbol.iterator] method.

his is the equivalent of a for/of in ES6
iterator = [1,2,3][Symbol.iterator]();
result;
e (!(result = iterator.next()).done) {
nsole.log(result.value); // 1 then 2 and then 3



his is the equivalent of a for/of in ES6
iterator = '??'[Symbol.iterator]();
result;
e (!(result = iterator.next()).done) {
nsole.log(result.value); // '?' first and '?' after

It is also possible to simply copy same iterator for any other iterable collection.

Caveats

There are few things developers need to know about Symbol partial polyfills. Here a quick summary.

null Objects

This polyfill will not work with null objects, and even if it's possible to make it work it's not worth the hassle.

o = Object.create(null); // or {__proto__: null}
s = Symbol();

 = 123;

ot set as Symbol, just as generic key
ct.keys(o); // [s]
the typeof gotcha

It is not possible to overwrite native typeof operator and while it returns symbol with native support, since version 0.5.0 it returns object when polyfilled. This is not perfect, but at least it's simple to distinguish between Symbols and regular properties in list of mixed properties collections.

cross-realm incompatibility

Symbol.for and Symbol.keyFor can't be shimmed cross-realm. To be extra fair, Symbol should never be used cross-realm unless natively supported.

is Symbol native ?

Since it's not possible to overwrite typeof, a check against typeof key === "symbol" is all we need to understand if support is native or not. Please note that transpilers might wrap this check so we should be sure the test is done natively and not before transpiling.

the in operator

Since it's also not possible to overwrite in, please note that Symbol() in {} is always true since SYmbols need to be shimmed through the Object.prototype.

How to use

Either npm install get-own-property-symbols or include this file on your page.

More details

There are alternatives to this polyfill Symbol only and the main difference is that whit get-own-property-symbols you actually have Object.getOwnPropertySymbols functionality and Object.getOwnPropertyNames will never show Symbols too.

Also today core-js brings Symbols in, but as part of the entire core-js partial polyfill, and with same caveats described in here.

Accordingly, if you are looking for a backward compatible, stand-alone version, as ES6 compliant as possible partial polyfill, use this module, otherwise feel free to pick alternatives.

Please note this polyfill is also compatible with Object.assign.

Compatibility Mobile Desktop Server

You can also check if your browser or device is compatible through this page.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.