Concepts and reasons for introduction

// A new proposed data structure Symbol base data type
// Symbol represents a unique seventh data type
ToString is implicitly called to convert an object whose property is a string to a string
let a = {a:1};
let b = {b:2};
let c = {};
c[a] = 3; // [object Object]
c[b] = 4; // [object Object]
console.log(c);
/ /? How to solve
let s = Symbol(a)console.log(typeof s); // The seventh general data type
Copy the code

Accept a string as an argument

let s1 = Symbol('a')
let s2 = Symbol('b')
console.log(s1);
console.log(s2);
console.log(s1.toString());
console.log(s2.toString());
let s3 = Symbol({})
console.log(s3);
// The returned values are not equal even if the values passed are the same
let s4 = Symbol(a)let s5 = Symbol(a);console.log(s4 === s5);
let s6 = Symbol('a')
let s7 = Symbol('a');
console.log(s6 === s7);
Copy the code

Cannot be evaluated with other types of values

let sym = Symbol('my Symbol')
'my symbol is' + sym
Copy the code

Symbol can be displayed as a string

let s1 = Symbol('a')
let s2 = Symbol('b')
console.log(s1);
console.log(s2);
console.log(s1.toString());
console.log(String(s1));
Copy the code

Symbol can be converted to a Boolean value but not to a value

let symb = Symbol(a);console.log( !Boolean(symb));
Copy the code

Symbol. The iterator attribute

The object'sSymbolIterator property refers to the method of the object's default traversatorfor. Called in the "of" loopSymbol.iterator
Copy the code

Object.getOwnPropertySymbols()

const obj = {};
let sym1 = Symbol('a')
let sym2 = Symbol('b')
obj[sym1] = 'hello'
obj[sym2] = 'world'
/ / Object. GetOwnPropertySymbols () returns an array of values are all used as a Symbol's value of the property name
const arr = Object.getOwnPropertySymbols(obj)
console.log(arr);
Copy the code

Function Instance constants enumerate private properties

// Private attributes are implemented
const private = Symbol('private')
const obj = {
    // Private attributes
    _name:'Joe',
    [private]:'Private properties'
}

console.log(Object.keys(obj));
Copy the code