File tree Expand file tree Collapse file tree 1 file changed +8
-8
lines changed
1-js/05-data-types/08-weakmap-weakset/01-recipients-read Expand file tree Collapse file tree 1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change 1
- 明智的选择是 ` WeakSet ` :
1
+ 让我们将已读消息存储在 ` WeakSet ` 中 :
2
2
3
- ``` js
3
+ ``` js run
4
4
let messages = [
5
- {text: " Hello" , from: " John" },
6
- {text: " How goes?" , from: " John" },
7
- {text: " See you soon" , from: " Alice" }
5
+ {text: " Hello" , from: " John" },
6
+ {text: " How goes?" , from: " John" },
7
+ {text: " See you soon" , from: " Alice" }
8
8
];
9
9
10
10
let readMessages = new WeakSet ();
@@ -14,18 +14,18 @@ readMessages.add(messages[0]);
14
14
readMessages .add (messages[1 ]);
15
15
// readMessages 包含两个元素
16
16
17
- // ... 让我们再读一遍第一条消息!
17
+ // …… 让我们再读一遍第一条消息!
18
18
readMessages .add (messages[0 ]);
19
19
// readMessages 仍然有两个不重复的元素
20
20
21
21
// 回答:message[0] 已读?
22
22
alert (" Read message 0: " + readMessages .has (messages[0 ])); // true
23
23
24
24
messages .shift ();
25
- // 现在 readMessages 有一个元素(技术上来说内存可能稍后被清理 )
25
+ // 现在 readMessages 有一个元素(技术上来讲,内存可能稍后才会被清理 )
26
26
```
27
27
28
- ` WeakSet ` 允许存储一系列的消息并且很容易就能检查它包含的消息是否还存在 。
28
+ ` WeakSet ` 允许存储一系列的消息,并且很容易就能检查它包含的消息是否还存在 。
29
29
30
30
它会自动清理自身。但是作为交换,我们不能对它进行迭代。我们不能直接获取所有已读消息。但是我们可以通过迭代所有消息然后找出存在于 set 的那些消息来完成这个功能。
31
31
You can’t perform that action at this time.
0 commit comments