Wednesday, December 28, 2022

Symbol

Symbol

The Symbol Constructor

Symbol ( [ description ] )

  1. If NewTarget is not undefined, throw a TypeError exception.
  2. Note: the Symbol Constructor is not intended to be used with the new operator(not the target for new)
  3. If description is undefined, let descString be undefined; Else, let descString be ? ToString(description).
  4. Return a new unique Symbol value whose [[Description]] value is descString.
  5. Note: the Symbol() function returns a Symbol type instead of a Symbol object

Symbol Prototype Object

abstract operation thisSymbolValue(value)

  1. If Type(value) is Symbol, return value.
  2. If Type(value) is Object and value has a [[SymbolData]] internal slot
  3. then Return value.[[SymbolData]] //a Symbol Object
  4. Throw a TypeError exception.

Example

mySymbol = Symbol( "my symbol" )

// Object() performs a type conversion when called as a function rather than as a constructor.
mySymObj = Object(mySymbol)

console.log(mySymObj.toString()) // Symbol(my symbol)

No comments:

Post a Comment