Skip to content

Commit

Permalink
fix(map-and-set): Symbol as WeakMap Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 3, 2023
1 parent 60c7676 commit 20e49dc
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions source/basic/map-and-set/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ obj = null;
`WeakMap``Map`と似ていますがiterableではありません。
そのため、キーを列挙する`keys`メソッドや、データの数を返す`size`プロパティなどは存在しません。
また、キーを弱い参照で持つ特性上、キーとして使えるのは参照型のオブジェクトだけです
また、キーを弱い参照で持つ特性上、キーとして使えるのは参照型のオブジェクトとSymbol[^es2023]だけです
`WeakMap`の主な使い方のひとつは、クラスにプライベートの値を格納することです。
`this` (クラスインスタンス) を `WeakMap` のキーにすることで、インスタンスの外からはアクセスできない値を保持できます。
Expand Down Expand Up @@ -513,7 +513,7 @@ console.log(results); // => ["a","b"]
つまり、`WeakSet`は値の追加と削除、存在確認以外のことができません。
データの格納ではなく、データの一意性を確認することに特化したセットと言えるでしょう。
また、弱い参照で値を持つ特性上、`WeakSet`の値として使えるのは参照型のオブジェクトだけです
また、弱い参照で値を持つ特性上、`WeakSet`の値として使えるのは参照型のオブジェクトとSymbolだけです
## まとめ {#conclusion}
Expand All @@ -535,3 +535,5 @@ console.log(results); // => ["a","b"]
[プロパティの存在を確認する]: ../object/README.md#confirm-property
[プロトタイプオブジェクト]: ../prototype-object/README.md
[`Object.prototype`を継承しないオブジェクト]: ../prototype-object/README.md#not-inherit-object
[^es2023]: ES2023でSymbolをキーとして扱えるように仕様が変更されています。

0 comments on commit 20e49dc

Please sign in to comment.