前端开发

js中Symbol的作用

2020-11-23  本文已影响0人  liuyeqing

唯一标识符,可用作对象的唯一属性值

// Here are two symbols with the same description:

letSym1=Symbol("Sym")

letSym2=Symbol("Sym")

console.log(Sym1===Sym2)// returns "false"// Symbols are guaranteed to be unique.// Even if we create many symbols with the same description,// they are different values.

letSym=Symbol("Sym")

alert(Sym.toString())    // Symbol(Sym), now it works

let_Sym=Symbol("Sym");

alert(_Sym.description);    // Sym

Symbol.keyFor(Symbol.for("tokenString"))==="tokenString"    // true

上一篇 下一篇

猜你喜欢

热点阅读