Asked 7 years ago
13 Jan 2017
Views 1116
jaydeep

jaydeep posted

what is WeakMap in JavaScript ?

what is use of WeakMap in JavaScript ?

    var deviceList = new WeakMap();


WeakMap is mapping regarding object or what ?

in which scenario WeakMap should be used ?
Rasi

Rasi
answered Nov 30 '-1 00:00

WeakMap
WeakMap is little brother of Map whose is weak .weak means it hold reference weakly so it easy for garbage Collector to remove it when needed.

one can use WeakMap when you dont need strong bond for reference so instances or reference is garbage collected.



var device = new Object("name");
 var deviceList = new WeakMap();
 deviceList .set(device,"mobile");



in this example devices instance and its data will be garbage collected when devices object is no longer referenced by the rest of the application.

on another word we can say its "Weak memory bond which will be cleared if not refereed any more "
what is map in JavaScript ? - jaydeep  
Jan 14 '17 01:22
Post Answer