基本数据类型: undefined nul boolean string number object
ES6 引入了一种新的原始数据类型 Symbol,表示独一无二的值。
javascript
{
// 声明
let a1 = Symbol()
let a2 = Symbol()
console.log(a1 === a2) // false
let a3 = Symbol.for('a3')
let a4 = Symbol.for('a3')
console.log(a3 === a4) // true
}
{
const a5 = Symbol('a5')
console.log(Symbol.keyFor(a5)) // undefined
const a6 = Symbol.for('a5')
console.log(Symbol.keyFor(a6)) // a5
}
{
let a1 = Symbol.for('abc')
let obj = {
[a1]: '123',
abc: 345,
c: 456,
}
console.log('obj', obj) // obj { abc: 345, c: 456, [Symbol(abc)]: '123' }
for (let [key, value] of Object.entries(obj)) {
console.log('let of', key, value)
// let of abc 345
// let of c 456
}
Object.getOwnPropertySymbols(obj).forEach(function (item) {
console.log(obj[item]) // 123
})
Reflect.ownKeys(obj).forEach(function (item) {
console.log('ownkeys', item, obj[item])
// ownkeys abc 345
// ownkeys c 456
})
}