篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Map Set WeakMap WeakSet相关的知识,希望对你有一定的参考价值。
/* Sets
Store unique values of any type,
*/
var s = new Set();
s.add("hello").add("goodbye").add("hello");
s.size === 2;
s.has("hello") === true;
/* Maps
key-value pairs and remembers the original
insertion order of the keys
*/
var m = new Map();
m.set("hello", 42);
m.set(s, 34);
m.get(s) == 34;
/* Weak Maps
keys are weakly referenced
keys must be objects and
the values can be arbitrary values
*/
var wm = new WeakMap();
wm.set(s, { extra: 42 });
wm.size === undefined
/* Weak Sets
Store weakly held objects in a collection */
var ws = new WeakSet();
ws.add({ data: 42 });
以上是关于javascript Map Set WeakMap WeakSet的主要内容,如果未能解决你的问题,请参考以下文章