存储阅读日期
重要性: 5
有一个消息数组,就像在上一个任务中一样。情况类似。
let
messages =
[
{
text:
"Hello"
,
from:
"John"
}
,
{
text:
"How goes?"
,
from:
"John"
}
,
{
text:
"See you soon"
,
from:
"Alice"
}
]
;
现在的问题是:您建议使用哪种数据结构来存储信息:“消息何时被读取”?
在上一个任务中,我们只需要存储“是/否”事实。现在我们需要存储日期,并且它应该只保留在内存中,直到消息被垃圾回收。
附注:日期可以存储为内置Date
类的对象,我们将在后面介绍。
要存储日期,我们可以使用WeakMap
let
messages =
[
{
text:
"Hello"
,
from:
"John"
}
,
{
text:
"How goes?"
,
from:
"John"
}
,
{
text:
"See you soon"
,
from:
"Alice"
}
]
;
let
readMap =
new
WeakMap
(
)
;
readMap.
set
(
messages[
0
]
,
new
Date
(
2017
,
1
,
1
)
)
;
// Date object we'll study later