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?
7
WeakMap
?Object
(which is the most common in Javascript)?WeakMap
and Map?5
I recommend reading the question about Map
first. The kind WeakMap
works exactly like the Map
, with the following main differences:
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
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));
Main differences:
get()
;set()
Weakmap, you need to use the operator =
;clear()
weakmap; Main differences:
forEach
, no longer Weakmap. string
, number
, etc), while Weakmap should be a Object
.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
How so Map
cannot retrieve the object?
You’re right @Jeffersonquesado, the Set
does not allow to recover, already corrected the answer, thanks for pointing
Browser other questions tagged javascript characteristic-language
You are not signed in. Login or sign up in order to post.
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?
– Wallace Maxters
Related: https://answall.com/q/314293/64969
– Jefferson Quesado