ES6 - Symbol.keyFor

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

语法

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

Symbol.keyFor(sym)

示例

<script>
const user_Id = Symbol.for('userId') // 在注册表中创建一个新符号
console.log(Symbol.keyFor(user_Id)) // 返回注册表中符号的键
const userId = Symbol("userId")// 符号不在注册表中
console.log(Symbol.keyFor(userId)) //userId 符号不在注册表中
</script>

代码输出如下所示 −

userId
undefined