javascript - What are ECMAScript 6 WeakMaps? -
after reading description: http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps
i'm trying hang of it, not overall picture. about? seems supported in firefox 6: http://kangax.github.com/es5-compat-table/non-standard/
weakmaps allow have hashtable key isn't string.
so can set key be, i.e. [1]
, can map.get([1])
example mdn:
var wm1 = new weakmap(), wm2 = new weakmap(); var o1 = {}, o2 = function(){}, o3 = window; wm1.set(o1, 37); wm1.set(o2, "azerty"); wm2.set(o1, o2); // value can anything, including object or function wm2.set(o3, undefined); wm2.set(wm1, wm2); // keys , values can objects. weakmaps! wm1.get(o2); // "azerty" wm2.get(o2); // undefined, because there no value o2 on wm2 wm2.get(o3); // undefined, because set value wm1.has(o2); // true wm2.has(o2); // false wm2.has(o3); // true (even if value 'undefined') wm1.has(o1); // true wm1.delete(o1); wm1.has(o1); // false
the reason existance is:
in order fix memory leak present in many uses of weak-key tables.
apparently emulating weakmaps causes memory leaks. don't know details of memory leaks.
Comments
Post a Comment