Symbol descriptions accessor
The symbol is one of datatype in javascript. toString() method outputs string form of Symbol object.
console.log(Symbol('desc').toString());//outputs Symbol(desc)
There is no way to get a description of the Symbol object before ES10. One way is to parse the string representation, the other way is using ES10 Symbol description.
with Es10, a new optional and read-only property description is added to the Symbol object, and the new accessor is added by providing a string as a description.
We can describe the creation of a symbol object for debugging the codes. ] There are many ways we can create a symbol description.
- Using Symbol constructor
- Symbol.for() method
Using Symbol constructor
Symbol object construction has an optional description, once created, which can be retrieved using the description parameter Syntax
const symbol=new Symbol(optionalDescription)
parameters are optional descriptions used for debugging the object
Here is an example of creating and using of description
const desc="Symbol description example"
const symbol = Symbol(desc);
console.log(symbol); //Symbol(Symbol description example)
console.log(symbol.description); //Symbol description example
console.log(symbol.toString()); //Symbol(Symbol description example)
Symbol.for().description method
Symbol.for(optionalDescription)
finds the symbol for a given key in the global registry, if founds returns the symbol, else create a new symbol.
// print global symbol description
Symbol.for('global desc').toString(); // "Symbol(global desc)"
Symbol.for('global desc').description; // "global desc"