weakmap

JavaScript(ES6) WeakMap garbage collection when set an object to null

北战南征 提交于 2019-11-28 12:23:01
I've just read that WeakMaps take advantage of garbage collection by working exclusively with objects as keys, and that assigning an object to null is equivalent to delete it: let planet1 = {name: 'Coruscant', city: 'Galactic City'}; let planet2 = {name: 'Tatooine', city: 'Mos Eisley'}; let planet3 = {name: 'Kashyyyk', city: 'Rwookrrorro'}; const lore = new WeakMap(); lore.set(planet1, true); lore.set(planet2, true); lore.set(planet3, true); console.log(lore); // output: WeakMap {{…} => true, {…} => true, {…} => true} Then I set the object equal to null: planet1 = null; console.log(lore); //

Creating a regular weak-reference in Javascript using WeakMaps

旧街凉风 提交于 2019-11-27 22:52:52
I am trying to do the obvious thing with WeakMaps: I want to create a weak reference. In particular, I want to have a list of event-listeners without that list influencing the life of the listener. So I was very excited to find WeakMaps, until I saw they were only built to satisfy one (fairly rare) use-case, extending objects that were otherwise sealed. I can't think when I ever wanted to do that, but I need lists of listeners all the time. Is this possible to use WeakMaps in some clever way I haven't thought of to do this? Bergi No, it is impossible to use WeakMaps to create a weak reference.

Why will ES6 WeakMap's not be enumerable?

六月ゝ 毕业季﹏ 提交于 2019-11-27 21:10:45
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?

JavaScript(ES6) WeakMap garbage collection when set an object to null

不问归期 提交于 2019-11-27 06:12:40
问题 I've just read that WeakMaps take advantage of garbage collection by working exclusively with objects as keys, and that assigning an object to null is equivalent to delete it: let planet1 = {name: 'Coruscant', city: 'Galactic City'}; let planet2 = {name: 'Tatooine', city: 'Mos Eisley'}; let planet3 = {name: 'Kashyyyk', city: 'Rwookrrorro'}; const lore = new WeakMap(); lore.set(planet1, true); lore.set(planet2, true); lore.set(planet3, true); console.log(lore); // output: WeakMap {{…} => true,

Creating a regular weak-reference in Javascript using WeakMaps

不打扰是莪最后的温柔 提交于 2019-11-26 21:13:14
问题 I am trying to do the obvious thing with WeakMaps: I want to create a weak reference. In particular, I want to have a list of event-listeners without that list influencing the life of the listener. So I was very excited to find WeakMaps, until I saw they were only built to satisfy one (fairly rare) use-case, extending objects that were otherwise sealed. I can't think when I ever wanted to do that, but I need lists of listeners all the time. Is this possible to use WeakMaps in some clever way

What are the actual uses of ES6 WeakMap?

梦想与她 提交于 2019-11-26 14:48:05
What are the actual uses of the WeakMap data structure introduced in ECMAScript 6? Since a key of a weak map creates a strong reference to its corresponding value, ensuring that a value which has been inserted into a weak map will never disappear as long as its key is still alive, it can't be used for memo tables, caches or anything else that you would normally use weak references, maps with weak values, etc. for. It seems to me that this: weakmap.set(key, value); ...is just a roundabout way of saying this: key.value = value; What concrete use cases am I missing? Benjamin Gruenbaum

What are the actual uses of ES6 WeakMap?

淺唱寂寞╮ 提交于 2019-11-26 04:02:00
问题 What are the actual uses of the WeakMap data structure introduced in ECMAScript 6? Since a key of a weak map creates a strong reference to its corresponding value, ensuring that a value which has been inserted into a weak map will never disappear as long as its key is still alive, it can\'t be used for memo tables, caches or anything else that you would normally use weak references, maps with weak values, etc. for. It seems to me that this: weakmap.set(key, value); ...is just a roundabout way