我一直在用
JavaScript工作
WeakMaps,在检查文档后我意识到明确的方法是
deprecated / removed from ECMAScript 6.
这是什么原因?
为什么强迫我们做一个明确的功能,如:
clear() { this._weakmap = new WeakMap() }
解决方法
“The mapping from weakmap/key pair value can only be observed or
affected by someone who has both the weakmap and the key. With
clear(),someone with only the WeakMap would’ve been able to affect
the WeakMap-and-key-to-value mapping.”
这种限制的原因是安全问题:
A key property of Weak Maps is the inability to enumerate their keys.
This is necessary to prevent attackers observing the internal behavior
of other systems in the environment which share weakly-mapped objects.
Should the number or names of items in the collection be discoverable
from the API,even if the values aren’t,WeakMap instances might
create a side channel where one was prevIoUsly not available.
可枚举的WeakMap也可能影响GC,因为您可以间接观察GC过程.因此,为了确保可预测的设计,也清除了清晰.