ES6 new data structure WeakSet method

Weakset and SET are similar, which is also a collection of elements, and their difference is that the elements in the weakset must be objects and cannot be other types.

Features:

1. The element must be an object.

Add a Number type element.

Const WS = New WeakSet () ws.add (1)
   The result is a type of error. 
TYPEERROR: INVALID VALUE Used in Weak Set

Add an object.


Const WS = New WeakSet () VAR A = {P1: ‘1’, P2: ‘2’} ws.add (a) console.log (WS. HAS (a));

Add OK, the result shows:
 True   can determine if an element can be judged by the HAS method of WeakSet Has been in the collection. 
If you do not need to store an element, you can use the Delete method to delete the element.

2. Weak references, not included in garbage collection

Add to WeakSet’s element object, WeakSet does not add 1 reference to elemental objects 1, for The element object to be added to the Weakset, as long as the element object is not quoted in other objects other than WeakSet, it will be released by garbage, and the element object in WeakSet is automatically released without memory leaks.

Because of this feature, its performance is higher than the MAP, which can be used for a presented, non-repetitive, temporary stored scene.

Const ws = new weakset () var A = {p1: ‘1’, p2: ‘2’} ws.add (a) a = nullconsole.log (ws.has (a))

First add the object into the weakset, then set the object to null, then the following HAS method is determined, the result is displayed, indicating that there is no need in Weakset. Object.

False

3. Because it is weakly referenced to the internal element object, it will be released by garbage recycling, so it Singing methods such as SIZE and FOREACH are not supported.
   
The above is a small-knitted data structure of the ES6 new data structure Weakset, I hope to help everyone, if everyone has any If you have questions, please leave a message, the small package will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment