
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Why do we need weakMaps in Javascript?
WeakMap is a collection in JavaScript. This type of collection is used to store the data in the form of key-value pairs. In WeakMap, the key must definitely be an object and the values can be of any type.
The difference between a Map and a WeakMap is, in weakmap key must be an object and the other difference is that a weakmap is like a blackbox where the keys can’t be retrieved.
The value of the weakmap can be accessed only if the key is known which means the values in the weakmap are private.
Additional data can be stored in a weakmap which is related to any object without changing the data in it and it also manages its memory too.
Need for the Weakmaps
Using WeakMaps we can achieve the following −
Storing the data of a class which is private
The private data of classes and the objects can be stored in weakmaps, as they are also called as black boxes. As there are many other ways in which storing of private data can be done, this is rarely used.
Example
// using object let wm = new WeakMap();// weakmap let student = {},//object student2 = {}; // set the data into weakmap wm.set(student, " private data"); wm.set(student2, "Private data 2"); // get the data from weakmap console.log(wm.get(student)); console.log(wm.get(student2)); // using class class studentdata { constructor() { wm.set(this, "private"); } printPrivate() { console.log(wm.get(this)); } } let s = new studentdata(); s.printPrivate();
Keeping track of DOM changes
Google Polymer Project is a library which provides some features to create custom elements and these features makes the customization easy and fast. So, this project uses the weakmaps to save the changes of DOM elements. Additional information of the DOM elements can also be stored.
In Caching
If array of elements is given as input and the sum of all the elements is calculated. Here, as the array is also an object it can also be stored as a key in the weakmap.
An important point to be remembered while in the case of weakmaps is that, if there is a scenario of keeping track of the additional information relating to the behaviour of the object, when
That no modification or addition of other properties should be done and
No special care is taken while memory management,
Then it is a scenario where the Weakmaps can be used without hesitation and a second thought.
- Related Articles
- Why do we need KDD?
- Why do we need Energy?
- Why do we need generics in Java?
- Why do we need Good Manners?
- Why do we need a Database
- Why do we need Computer Networks?
- Why do we need shell scripting?
- Why do we need Data Encryption?
- Why do we need inner classes in Java?
- Why do we need to study Physics?
- Why do we need to store food?
- Why do we need a wrapper class in Java?
- Why do we need a separate Data Warehouse?
- Do we need to use semicolons in JavaScript?
- Why do we need a pure virtual destructor in C++?
