Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(map-and-set): Symbol as WeakMap Keys #1684

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]だけです
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

脚注にしているけど ES2023 でも別に問題ないかな。
SymbolがES2023のものに見えるのを回避したいぐらい


`WeakMap`の主な使い方のひとつは、クラスにプライベートの値を格納することです。
`this` (クラスインスタンス) を `WeakMap` のキーにすることで、インスタンスの外からはアクセスできない値を保持できます。
Expand Down Expand Up @@ -513,7 +513,7 @@ console.log(results); // => ["a","b"]
つまり、`WeakSet`は値の追加と削除、存在確認以外のことができません。
データの格納ではなく、データの一意性を確認することに特化したセットと言えるでしょう。

また、弱い参照で値を持つ特性上、`WeakSet`の値として使えるのは参照型のオブジェクトだけです
また、弱い参照で値を持つ特性上、`WeakSet`の値として使えるのは参照型のオブジェクトと`Symbol`[^es2023]だけです

## まとめ {#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も扱えるように仕様が変更されています。