Es6-Symbol-for
提供:Dev Guides
ES6-Symbol.for()
この関数は、シンボルを作成してレジストリに追加します。 シンボルがすでにレジストリに存在する場合、同じものを返します。それ以外の場合は、新しいシンボルがグローバルシンボルレジストリに作成されます。
構文
Symbol.for(key)
ここで、 key はシンボルの*識別子*です
例
次の例は、* Symbol()と Symbol.for()*の違いを示しています。
<script>
const userId = Symbol.for('userId')//creates a new Symbol in registry
const user_Id = Symbol.for('userId')//reuses already created Symbol
console.log(userId == user_Id)
const studentId = Symbol("studentID")//creates symbol but not in registry
const student_Id = Symbol.for("studentID")//creates a new Symbol in registry
console.log(studentId == student_Id)
</script>
上記のコードの出力は以下のようになります-
true
false