ES6 - Symbol.keyFor



此方法从全局符号注册表中检索给定符号的共享符号键。

语法

Symbol.keyFor 的语法如下,其中 sym 是用于查找键的符号。

Symbol.keyFor(sym)

示例

<script>
   const user_Id = Symbol.for('userId') // creates a new Symbol in registry
   console.log(Symbol.keyFor(user_Id)) // returns the key of a symbol in registry
   const userId = Symbol("userId")// symbol not in registry
   console.log(Symbol.keyFor(userId)) //userId symbol is not in registry
</script>

输出的代码如下所示 -

userId
undefined
广告
© . All rights reserved.