1 //Symbol生成一个独一无二的值,生成的值不会相等 2 { 3 //声明1 4 let a1=Symbol(); 5 let a2=Symbol(); 6 console.log(a1===a2);//false 7 //声明2 8 let a3=Symbol.for('a3'); 9 let a4=Symbol.for('a3'); 10 console.log(a3===a4);//true 11 } 12 { 13 let a1=Symbol.for('abc'); 14 let obj={ 15 [a1]:'123', 16 'abc':345, 17 'c':456 18 }; 19 console.log('obj',obj) 20 //普通的遍历,得不到symbol的值 21 for(let [key,value] of Object.entries(obj)){ 22 console.log(key,value) 23 } 24 //Object.getOwnPropertySymbols可以获取到symbol的值 25 Object.getOwnPropertySymbols(obj).forEach(function(item){ 26 console.log(obj[item]) 27 }) 28 //Reflect.ownKeys,返回所有的,包含symbol和非symbol 29 Reflect.ownKeys(obj).forEach(function(item){ 30 console.log(item,obj[item]) 31 }) 32 }