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
廣告