let s = Symbol('foo') let ss = Symbol('foo') s === ss // false
let name = Symbol('name'); let fun = Symbol('fun'); let obj = { [name]:zhan [fun](){ // doSomething } }
Object.getOwnPropertySymbols獲取該Symbol屬性html
消除魔術字符創angular2
const shapeType = { triangle: Symbol('triangle'), other: Symbol('other') }; function getArea(shape, options) { let area = 0; switch (shape) { case shapeType.triangle: area = .5 * options.width * options.height; break; case shapeType.other: // do something break; } return area; } getArea(shapeType.triangle, { width: 100, height: 100 })
let s1 = Symbol.for('foo'); let s2 = Symbol.for('foo'); s1 === s2
let s1 = Symbol.for("foo"); Symbol.keyFor(s1) // "foo" let s2 = Symbol("foo"); Symbol.keyFor(s2) // undefine 變量s2屬於未登記的 Symbol 值,因此返回undefined