What’s the Weakmap object for?

Asked

Viewed 209 times

7

  • How the object works WeakMap?
  • What’s the difference between him and a Object (which is the most common in Javascript)?
  • What’s the difference between WeakMap and Map?
  • Which browsers can I use?
  • Who gave the negatives could explain? What can be improved on the question? Or is it simply because it disagrees with the way I ask?

  • Related: https://answall.com/q/314293/64969

2 answers

5


I recommend reading the question about Map first. The kind WeakMap works exactly like the Map, with the following main differences:

  1. Only accepts objects as key
  2. It is not enumerable, that is, it does not allow to list keys
  3. Key references are always weak references

The latter is in bold because it is the reason for WeakMap exist. A "weak reference" in this context means that the use of a particular object as a key in a WeakMap doesn’t count from the point of view of the garbage collector. If there is no other reference to an object used as a key in a WeakMap, the value corresponding to that key in WeakMap is released to be collected by Garbage Collector.

4

How the Weakmap object works?

WeakMap is an object of Javascript which provides an object dictionary, i.e., an object that stores data in the key/value format.

As a dictionary, it works based on a key, i.e., for include/recover/delete operations is using a key to identify the object.

Here is an example of the creation of a dictionary and the basic operations:

var dic = new WeakMap()

//chaves
var chaveA = { chave: 'A' };
var chaveB = { chave: 'B' };
var chaveC = { chave: 'C' };

// adicionando objetos
dic.set(chaveA, 1);
dic.set(chaveB, "dois");
dic.set(chaveC, new Date(2018,06,13));

// verificando objetos
console.log("existe objeto 'A':" + dic.has(chaveA));

// recuperando
console.log("valor do objeto 'A':" + dic.get(chaveA));
console.log("valor do objeto 'B':" + dic.get(chaveB));
console.log("valor do objeto 'C':" + dic.get(chaveC));

// excluindo
dic.delete(chaveA);
console.log("existe objeto 'A':" + dic.has(chaveA));

What is the difference between it and an Object (which is the most common in Javascript)?

Main differences:

  • does not have a method to retrieve an object, as Weakmap has the get();
  • does not have a method for assigning values such as set() Weakmap, you need to use the operator =;
  • does not have a method for cleaning properties such as clear() weakmap;

What is the difference between Weakmap and Map?

Main differences:

  • Map is iterable and therefore supports forEach, no longer Weakmap.
  • The key of a Map object can be any object (string, number, etc), while Weakmap should be a Object.

Which browsers can I use?

According to the Developer.mozila.org on 13/07/2018 compatibility is:

Chrome  Firefox     Internet Explorer   Opera           Safari
36      6.0 (6.0)   11                  Não suportado   Não suportado

Developer.mozilla.org

  • 2

    How so Map cannot retrieve the object?

  • 1

    You’re right @Jeffersonquesado, the Set does not allow to recover, already corrected the answer, thanks for pointing

Browser other questions tagged

You are not signed in. Login or sign up in order to post.