ES6 Symbol
表示一个独一无二的值
const sym = Symbol()
不需要new
- unique
1
2
3const s1 = Symbol('tan')
const s2 = Symbol('tan')
console.log(s1 === s2); // error - 不能使用运算符
1
2
3const s3 = Symbol(100)
console.log(s3 + 200); // TS2365: Operator '+' cannot be applied to types 'unique symbol' and 'number'.
// 不能使用运算符 或 + 拼接 - toString()
1
console.log(s3.toString() + 200); // toString() + 拼接 "Symbol(100)200"
作为对象的属性
1 | let s4 = Symbol('name') |