20.4 Symbol Objects
20.4.1 The Symbol Constructor
The Symbol
- is %Symbol%.
- is the initial value of the
"Symbol" property of theglobal object . - returns a new Symbol value when called as a function.
- is not intended to be used with the
new
operator. - is not intended to be subclassed.
- may be used as the value of an
extends
clause of a class definition but asuper
call to it will cause an exception.
20.4.1.1 Symbol ( [ description ] )
This function performs the following steps when called:
- If NewTarget is not
undefined , throw aTypeError exception. - If description is
undefined , let descString beundefined . - Else, let descString be ?
ToString (description). - Return a new Symbol whose [[Description]] is descString.
20.4.2 Properties of the Symbol Constructor
The Symbol
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
20.4.2.1 Symbol.asyncIterator
The initial value of Symbol.asyncIterator
is the well known symbol
This property has the attributes { [[Writable]]:
20.4.2.2 Symbol.for ( key )
This function performs the following steps when called:
- Let stringKey be ?
ToString (key). - For each element e of the GlobalSymbolRegistry
List , do- If
SameValue (e.[[Key]], stringKey) istrue , return e.[[Symbol]].
- If
Assert : GlobalSymbolRegistry does not currently contain an entry for stringKey.- Let newSymbol be a new Symbol whose [[Description]] is stringKey.
- Append the
Record { [[Key]]: stringKey, [[Symbol]]: newSymbol } to the GlobalSymbolRegistryList . - Return newSymbol.
The GlobalSymbolRegistry is an append-only
20.4.2.3 Symbol.hasInstance
The initial value of Symbol.hasInstance
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.4 Symbol.isConcatSpreadable
The initial value of Symbol.isConcatSpreadable
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.5 Symbol.iterator
The initial value of Symbol.iterator
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.6 Symbol.keyFor ( sym )
This function performs the following steps when called:
- If sym
is not a Symbol , throw aTypeError exception. - Return
KeyForSymbol (sym).
20.4.2.7 Symbol.match
The initial value of Symbol.match
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.8 Symbol.matchAll
The initial value of Symbol.matchAll
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.9 Symbol.prototype
The initial value of Symbol.prototype
is the
This property has the attributes { [[Writable]]:
20.4.2.10 Symbol.replace
The initial value of Symbol.replace
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.11 Symbol.search
The initial value of Symbol.search
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.12 Symbol.species
The initial value of Symbol.species
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.13 Symbol.split
The initial value of Symbol.split
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.14 Symbol.toPrimitive
The initial value of Symbol.toPrimitive
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.15 Symbol.toStringTag
The initial value of Symbol.toStringTag
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.2.16 Symbol.unscopables
The initial value of Symbol.unscopables
is the well-known symbol
This property has the attributes { [[Writable]]:
20.4.3 Properties of the Symbol Prototype Object
The Symbol prototype object:
- is %Symbol.prototype%.
- is an
ordinary object . is not a Symbol instance and does not have a [[SymbolData]] internal slot.- has a [[Prototype]] internal slot whose value is
%Object.prototype% .
20.4.3.1 Symbol.prototype.constructor
The initial value of Symbol.prototype.constructor
is
20.4.3.2 get Symbol.prototype.description
Symbol.prototype.description
is an
- Let s be the
this value. - Let sym be ?
ThisSymbolValue (s). - Return sym.[[Description]].
20.4.3.3 Symbol.prototype.toString ( )
This method performs the following steps when called:
- Let sym be ?
ThisSymbolValue (this value). - Return
SymbolDescriptiveString (sym).
20.4.3.3.1 SymbolDescriptiveString ( sym )
The abstract operation SymbolDescriptiveString takes argument sym (a Symbol) and returns a String. It performs the following steps when called:
- Let desc be sym's [[Description]] value.
- If desc is
undefined , set desc to the empty String. Assert : descis a String .- Return the
string-concatenation of"Symbol(" , desc, and")" .
20.4.3.4 Symbol.prototype.valueOf ( )
This method performs the following steps when called:
- Return ?
ThisSymbolValue (this value).
20.4.3.4.1 ThisSymbolValue ( value )
The abstract operation ThisSymbolValue takes argument value (an
- If value
is a Symbol , return value. - If value
is an Object and value has a [[SymbolData]] internal slot, then- Let s be value.[[SymbolData]].
Assert : sis a Symbol .- Return s.
- Throw a
TypeError exception.
20.4.3.5 Symbol.prototype [ @@toPrimitive ] ( hint )
This method is called by ECMAScript language operators to convert a Symbol object to a primitive value.
It performs the following steps when called:
- Return ?
ThisSymbolValue (this value).
The argument is ignored.
This property has the attributes { [[Writable]]:
The value of the
20.4.3.6 Symbol.prototype [ @@toStringTag ]
The initial value of the
This property has the attributes { [[Writable]]:
20.4.4 Properties of Symbol Instances
Symbol instances are
20.4.5 Abstract Operations for Symbols
20.4.5.1 KeyForSymbol ( sym )
The abstract operation KeyForSymbol takes argument sym (a Symbol) and returns a String or