Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why will ES6 WeakMap's not be enumerable?

Before my re-entry in JavaScript (and related) I've done lots of ActionScript 3 and there they had a Dictionary object that had weak keys just like the upcoming WeakMap; but the AS3 version still was enumerable like a regular generic object while the WeakMap specifically has no .keys() or .values().

The AS3 version allowed us to rig some really interesting and usefull constructs but I feel the JS version is somewhat limited. Why is that?

If the Flash VM could do it then what is keeping browsers from doing same? I read how it would be 'non-deterministic' but that is sort of the point right?

like image 462
Bartvds Avatar asked Dec 11 '13 01:12

Bartvds


People also ask

Why are WeakMap keys not enumerable?

A WeakMap can be a particularly useful construct when mapping keys to information about the key that is valuable only if the key has not been garbage collected. But because a WeakMap doesn't allow observing the liveness of its keys, its keys are not enumerable. There is no method to obtain a list of the keys.

What are the actual uses of ES6 WeakMap?

WeakMap is a new Data Structure or Collection introduced in ES6. WeakMaps allows you to store a collection of Key-Value pairs. It adopts the same properties of Map. The Major difference is that keys of WeakMap cannot be a primitive data type.

What is not true about a WeakMap?

WeakMap does not support iteration and methods keys() , values() , entries() , so there's no way to get all keys or values from it.


1 Answers

Finally found the real answer: http://tc39wiki.calculist.org/es6/weak-map/

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.

like image 120
Bartvds Avatar answered Sep 22 '22 05:09

Bartvds