JS: Symbol Object

By Xah Lee. Date: . Last updated: .

New in ES2015.

Symbol is the value of the property key "Symbol" of the global object.

console.log( window["Symbol"] === Symbol ); // true

Symbol is a function.

[see JS: Data Types]

// type of Symbol
console.log ( typeof Symbol === "function" ); // true
console.log ( Object.prototype.toString.call(Symbol) === "[object Function]" ); // true

The parent of Symbol is Function.prototype. [see JS: Prototype and Inheritance]

// parent of Symbol
console.log ( Object.getPrototypeOf ( Symbol ) === Function.prototype ); // true

The purpose of Symbol is:

  1. To create values of symbol type.
  2. Holds value properties and function properties that are useful for symbol values.
  3. Holds the property Symbol.prototype, which is the parent of all symbol objects.

[see JS: Symbol Tutorial]

[see JS: Data Types]

Symbol Constructor

Properties

  1. Symbol.prototype
  1. Symbol.for ( key )
  2. Symbol.hasInstance
  3. Symbol.isConcatSpreadable
  4. Symbol.iterator
  5. Symbol.keyFor ( sym )
  6. Symbol.match
  7. Symbol.replace
  8. Symbol.search
  9. Symbol.species
  10. Symbol.split
  11. Symbol.toPrimitive
  12. Symbol.toStringTag
  13. Symbol.unscopables

2017-02-10 following is work in progress.


2017-02-10 following is work in progress.


2017-02-10 following is work in progress.


2017-02-10 following is work in progress.


2017-02-10 following is work in progress.

Symbol.prototype

JS: Symbol.prototype

Symbol.for ( key )

Symbol.for(string) → access shared symbols registry.

ECMAScript® 2016 Language Specification#sec-symbol.for

Symbol.hasInstance

ECMAScript® 2016 Language Specification#sec-symbol.hasinstance

Symbol.isConcatSpreadable

ECMAScript® 2016 Language Specification#sec-symbol.isconcatspreadable

Symbol.iterator

ECMAScript® 2016 Language Specification#sec-symbol.iterator

Symbol.keyFor ( sym )

ECMAScript® 2016 Language Specification#sec-symbol.keyfor

Symbol.match

ECMAScript® 2016 Language Specification#sec-symbol.match

Symbol.replace

ECMAScript® 2016 Language Specification#sec-symbol.replace

Symbol.search

ECMAScript® 2016 Language Specification#sec-symbol.search

Symbol.species

ECMAScript® 2016 Language Specification#sec-symbol.species

Symbol.split

ECMAScript® 2016 Language Specification#sec-symbol.split

Symbol.toPrimitive

ECMAScript® 2016 Language Specification#sec-symbol.toprimitive

Symbol.toStringTag

ECMAScript® 2016 Language Specification#sec-symbol.tostringtag

Symbol.unscopables

ECMAScript® 2016 Language Specification#sec-symbol.unscopables


Well-Known (Predefined) Symbols

Well-Known symbols are symbols predefined by the js standard.

Well-known symbols are built-in Symbol values They are typically used as the keys of properties whose values serve as extension points. Unless otherwise specified, well-known symbols values are shared by all Code Realms.

Well-Known symbols are denoted with 2 at signs in front, like this: @@iterator

Symbol.iterator

the operator instanceof is now a method of function, with property key [Symbol.hasInstance]. someting like o instanceof f same as f[Symbol.hasInstance](o). this makes it extensible. todo

Symbol.unscopables

Symbol.match method for customizing str.match(obj). todo

Well-known Symbols
Specification Name [[Description]] Value and Purpose
@@hasInstance "Symbol.hasInstance" A method that determines if a constructor object recognizes an object as one of the constructor’s instances. Called by the semantics of the instanceof operator.
@@isConcatSpreadable "Symbol.isConcatSpreadable" A Boolean valued property that if true indicates that an object should be flattened to its array elements by Array.prototype.concat.
@@iterator "Symbol.iterator" A method that returns the default Iterator for an object. Called by the semantics of the for-of statement.
@@match "Symbol.match" A regular expression method that matches the regular expression against a string. Called by the String.prototype.match method.
@@replace "Symbol.replace" A regular expression method that replaces matched substrings of a string. Called by the String.prototype.replace method.
@@search "Symbol.search" A regular expression method that returns the index within a string that matches the regular expression. Called by the String.prototype.search method.
@@species "Symbol.species" A function valued property that is the constructor function that is used to create derived objects.
@@split "Symbol.split" A regular expression method that splits a string at the indices that match the regular expression. Called by the String.prototype.split method.
@@toPrimitive "Symbol.toPrimitive" A method that converts an object to a corresponding primitive value. Called by the ToPrimitive abstract operation.
@@toStringTag "Symbol.toStringTag" A String valued property that is used in the creation of the default string description of an object. Accessed by the built-in method Object.prototype.toString.
@@unscopables "Symbol.unscopables" An object valued property whose own property names are property names that are excluded from the with environment bindings of the associated object.

Reference

ECMAScript® 2016 Language Specification#sec-symbol-objects

Symbol Topic

  1. JS: Symbol Tutorial
  2. JS: Symbol Object
  3. JS: Symbol.prototype
Liket it? Put $1 at patreon.

Or, Buy JavaScript in Depth