This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge
ES6 introduces a new primitive data type, symbol, which represents unique values. This can be detected by typeof.
There are seven types of data that can be detected by typeof: number, String, Boolean, undefined, Object, function and the new symbol
Variables of type symbol are generated using the symbol function
let sym = Symbol(a);typeof sym;//symbol
Copy the code
Symbol is a function, but is not a constructor, so it cannot be new; Uncaught TypeError: Symbol is not a constructor
typeof Symbol//function
new Symbol//Uncaught TypeError: Symbol is not a constructor
Copy the code
Symbol can be used as a key for an object, ensuring that it does not conflict with other attribute names. But put variables in parentheses []
let sym1 = Symbol(a);let sym2 = Symbol(a);let obj = {
[sym1]: 'aaa'
}
obj[sym2] = "bbb"
console.log(obj[sym1])//aaa
console.log(obj[sym2])//bbb
Copy the code
Variables of type symbol are generated by symbol and are unique, so they are not equal
let sym1 = Symbol(a);let sym2 = Symbol(a); sym1 == sym2//false
Copy the code
In order to distinguish between the symbol variables, the symbol function takes a parameter that describes the value of the symbol variable
let s1 = Symbol('this is s1');
let s2 = Symbol('this is the s2');
console.log(s1.toString());/ / 'Symbol (this is s1)'
console.log(s2.toString());/ / 'Symbol (this is the s2)'
Copy the code
Even if the same description is generated, the symbol variable is not equal
Symbol('aaa') = =Symbol('aaa')//false
Copy the code
Symbol. Prototype has a description attribute that makes it easier to get a description of the Symbol variable
const sym = Symbol('foo');
sym.description // "foo"
Copy the code
The symbol variable is used as the attribute name, and is passed through the for… In, for… Of and Object. The keys (), Object. GetOwnPropertyNames (), JSON. The stringify () cannot traverse, but it is not the private property, also have a Object. The getOwnPropertySymbols () method, Gets all of the object’s symbol type attribute names
let a = Symbol('a');
let b = Symbol('b');
const obj = {
[a] : 'apple',
[b] : 'banana'
};
const objectSymbols = Object.getOwnPropertySymbols(obj);
console.log(objectSymbols)// [Symbol(a), Symbol(b)]
Copy the code
Sometimes we want the same description to have the same value. We can do this by using Symbol.for(), which takes a string as an argument and searches for any Symbol with that parameter as its name. If so, return the Symbol value, otherwise create a new Symbol with the string name and register it globally
let s1 = Symbol.for('aaa');
let s2 = Symbol.for('aaa');
s1 === s2 // true
Copy the code
So multiple calls to Symbol. For (” XXX “) will return the same Symbol value each time, but multiple calls to Symbol(” XXX “) will return different Symbol values;
You can use symbol.keyfor () to return the key of a registered Symbol value. Unregistered Symbol value, so return undefined
let s1 = Symbol.for("foo");
Symbol.keyFor(s1) // "foo"
let s2 = Symbol("foo");
Symbol.keyFor(s2) // undefined
Copy the code
So that’s what symbol is about!