Symbol type

Symbol’s new primitive data type (Symbol) represents a unique value

The key of the object can use Symbol

New Symbol() is not supported with static properties and static methods

Resolve to use common variables for different files and interact with each other,

//shared.js ================================== const cache = {} //a.js ====================================== / / cache/' a_foo '| | cache [' b_foo] before practice, agree on key under different files, But did not solve the problem essentially just avoid the problem of cache [' foo '] = "VLAUE" / / b.j s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = cache [' foo '] = "VALUE1111"
const s = Symbol() console.log(s); //Symbol() console.log(typeof s); //symbol console.log(Symbol() === Symbol(),Symbol("aa") === Symbol("aa")); //false false console.log(Symbol("aa"),Symbol("bb")) // describe the text Symbol(aa) Symbol(bb)

Can be added as a property name

const obj1 = {
  [Symbol()]:123
}
console.log(obj1);

case

// create private variable const name = Symbol() const person = {[name]:"zxa", say(){console.log(this[name]); } // person[Symbol()] // Person [Symbol()] // Person [Symbol()] // Person [Symbol()] // Person [Symbol()

A static method

Const s1 = Symbol. For () // A global registry is maintained internally to provide a one-to-one correspondence between the added identifier string and the Symbol value. Const s2 = symbol. For () console.log(s1 === s2); //true if the description string is added, the added description string must be consistent true console.log(Symbol.for(true) === = Symbol.for("true")); // True is converted to a string

Notice that Symbol is not available as key

console.log(cache); //{ foo: 'VALUE1111', [Symbol()]: 100, [Symbol()]: 200 } for (var key in cache){ console.log(key); // ['foo'] console.log(json.stringify (cache)); / / {" foo ":" VALUE1111} / / alone take Symbol () method to the console. The log (Object. GetOwnPropertySymbols (cache)); //[ Symbol(), Symbol() ]