This is the fourth day of my participation in Gwen Challenge

Symbol

We all know that es5’s object attribute names are strings, which makes it easy to have conflicting attribute names. For es6, Symbol was introduced, a new primitive data type that represents unique values.

Let Symbol = Symbol()

We cannot use the new command before the PS: Symbol function, or we will get an error. This is because the generated Symbol is a primitive value, not an object.

The Symbol function can take a string as an argument representing a description of the Symbol instance, mainly to make it easier to distinguish when displayed on the console or converted to a string.

If Symbol’s argument is an object, the object’s toString method is called and converted to a string before a Symbol value is generated.

The Symbol value cannot be manipulated with other types of values and will return an error. However, it can be explicitly converted to a string. In addition, it can be converted to a Boolean value, but not to a numeric value.

Symbol.prototype.description

You can add a description when you create a symbol. To read its description, we can use the description property added in ES2019.

Symbol is used as the attribute name

Since Symbol is a unique value, it means that when used as the attribute name of an object, it is guaranteed that no attribute with the same name will appear. The dot operator cannot be used when the ps: Symbol value is used as an object attribute name

Traversal of attribute names

Symbol is the attribute name. When traversing the object, the attribute does not appear in the for… In, for… Of loop, will not be the Object. The keys (), Object, getOwnPropertyNames (), JSON. The stringify () returns.

However, it is not private property, with an Object. The getOwnPropertySymbols () method, which can get all the Symbol of specified Object attribute names. This method returns an array of all the Symbol values used as attribute names for the current object.

const obj = {};
let a = Symbol('a');
let b = Symbol('b');

obj[a] = 'Hello';
obj[b] = 'World';

const objectSymbols = Object.getOwnPropertySymbols(obj);

objectSymbols
// [Symbol(a), Symbol(b)]
Copy the code

The new API reflection.ownkeys () method can return all types of key names, including regular and Symbol.

Because the Symbol value is used as the key name, it is not iterated by the normal method. We can take advantage of this feature to define non-private methods for objects that we want to use only internally.

Symbol. The for (), Symbol. KeyFor ()

Sometimes, we want to reuse the same Symbol value, and the symbol.for () method can do this. It takes a string as an argument and searches for a Symbol value with that argument as its name. If so, return the Symbol value, otherwise create a new Symbol with the name of the string and register it globally.

Symbol. For () and Symbol() both generate a new Symbol. The difference is that the former will be registered in the global environment for search, while the latter will not.

let s1 = Symbol.for('foo');
let s2 = Symbol.for('foo');

s1 === s2 // true
Copy the code

Symbol.for() registers the name of the Symbol value for the global environment, whether or not it is running in the global environment.

The symbol.keyfor () method returns the key of a registered Symbol type value.

let s1 = Symbol.for("foo");
Symbol.keyFor(s1) // "foo"

let s2 = Symbol("foo");
Symbol.keyFor(s2) // undefined
Copy the code