1, Symbol creates a unique value, the result must be different

let n = Symbol(),
m = Symbol();
console.log(n === m); //false

n = Symbol('A');
m = Symbol('A');
console.log(n === m); //false 
Copy the code

2. After assigning a value to a variable, it must be enclosed in [] as an attribute, otherwise it will not take effect

let n = Symbol('N'); Let obj = {age: 11, age: 11, [n]: 100};Copy the code

3, Application: eliminate magic strings

// Macro management: eliminate magic string const vote_plus = Symbol('vote_plus'); function reducer(action) { let state = { count: 0 }; switch (action.type) { case vote_plus: state.count++; break; } return state; } reducer({ type: vote_plus });Copy the code

4. Symbol is a constructor function, but cannot be new. Executing new will result in an error

(1) A value of type Symbol is not an instance of Symbol

(2) Prove once again that the value of symbol type is a primitive value(3) We can use Object() to convert a value of type symbol to an instance of type Object

(4) The value of symbol type cannot be used in the operation

Implicit type conversions are not possibleCopy the code

Explicit type conversions are possibleCopy the code

5, do not participate in the Symbol for… In/of traversal

For in the iteration/Object. Keys/Object. GetOwnPropertyNames/JSON stringify cannot traverse Symbol attribute

for (let key in obj) {
    console.log(key);
} 
console.log(JSON.parse(JSON.stringify(obj)));

let [n, m] = Object.getOwnPropertySymbols(obj);
console.log(obj[n]);
Object.getOwnPropertySymbols(obj).forEach(key => {
    console.log(obj[key]);
}); 
Copy the code

Object. GetOwnPropertyNames access is an enumerable and an enumeration

Keys is enumerable

ES6 provides a number of built-in Symbol values that point to methods used within the language

(1) Each constructor is an instance of Function, so each constructor can call the symbol. hasInstance method (2) The principle of Instanceof

(3) Symbol. IsConcatSpreadable: value is a Boolean, said that the object is used to Array. The prototype. The concat (), whether can be expanded

let arr1 = [1, 2, 3],
arr2 = [4, 5, 6];
console.log(arr2[Symbol.isConcatSpreadable]); //undefined
console.log(arr1.concat(arr2));
Copy the code

arr1[Symbol.isConcatSpreadable] = false;
arr2[Symbol.isConcatSpreadable] = false;
console.log(arr1.concat(arr2)); 
Copy the code

Iterator can be iterated as long as the prototype of the class to which the instance belongs has the Symbol. Iterator attribute above; All iterable objects can be looped through for ofThe Object prototype does not have this property, so we can add this property to the Object we are iterating over(5) symbol. toPrimitive: This method is called when the object is converted to a value of the original type and returns the corresponding value of the original type of the object

Obj [symbol.toprimitive] (hint) if there is a hint that we want to convert to by default

The underlying process of converting an object type to a number or string

Exercise: When a why is the following true

 if (a == 1 && a == 2 && a == 3) {
    console.log('OK');
 }
Copy the code

This involves casting data types: juejin.cn/post/692047…

(6) Symbol. ToStringTag: call the Object on the Object. The prototype. The toString method, if the property exists, it returns a value will appear in the toString () method returns the string, said the type of Object

Object Data type detection

We ourselves define the detection result of an object like this:We also want the result to be “[Object Person]” as above and we want it to look something like this