This is the 21st day of my participation in Gwen Challenge
Relearn JavaScript in a series of articles…
Solve the title problem first, and then learn basic knowledge, know its cause and effect. Look at this code:
Var set = {a:1} var set = new set () set.add(obj) obj = null Console. log(set.size()) // retrieve the value and see output: div#set, not null [...set][0]Copy the code
In the example, when the variable obj=null, the reference to the original object is cleared, but the Set ensures that the reference remains, which can be obtained by expanding the operator. Also because of the Set reference to obj, when obj=null, the garbage collection mechanism cannot free the object to which the initial variable obj points. Weak Set If you want to eliminate references from a Set and send messages to the references in the Set, you need to use Weak Set. Weak Set is a Weak Set that stores only Weak references to objects and does not store raw values. Weak references in a collection that are unique to an object are reclaimed and freed.
Var weakSet = {a:1} var weakSet = new weakSet () weakset.add (obj) // Clear reference obj = null // {} weak set reference is also deleted console.log(weakSet)Copy the code
After answering the questions, look at the corresponding usage methods.
The Set collection
Set is an ordered list, which contains some independent non-repeating values. Through Set, the data can be accessed quickly and various discrete values can be tracked more effectively.
Set itself is a constructor that can take an array or array-like object as an argument. Let’s look at its properties and functions:
attribute
Set. The prototype. The constructor: the constructor
Set.prototype.size: Returns the total number of members of the instance
methods
Set.prototype.add(value) : Adds a value and returns the Set structure itself
Set.prototype.delete(vallue) : Deletes a value and returns a Boolean value
Set.prototype.has(value) : Returns a Boolean value indicating whether it is a member
Set.prototype.clear() : clear all members, no return value
usage
- The add,
Add adds a new value, which is strictly equal to the original value (“===”), and only unequal values can be added to the Set instance. One exception: NaN, because NaN and NaN are not equal under strict equality comparison, but there can only be one NaN in a Set.
var set = new Set() set.add(1) set.add('1') set.add(1) // 1==='1' -> false console.log(set) // Set(2) {1, Var set1 = new Set() set1.add(NaN) set1.add(NaN) console.log(set1) // Set(1) {NaN} added 2 NaN, Var set = new set ([1,2,3,4]) set.delete(1) Set :{2,3,4} set.has(3) // true set:{2,3,4} set.has(3) // trueCopy the code
- Array to heavy
/ / a one-dimensional array to heavy var arr =,1,2,2,3 [1] var set = new set (arr) console. The log (set) / / {1, 2, 3} / / most heavy and var arr1 = [1, 2, 3] var Var arr2 = [1, 2, 3] characters = new Set ([arr1,... arr2]) console. The log (characters) / / {1, 2, 3}Copy the code
- Set to array conversion
Var set = new set (arr) array. from var set = new set () set.add(1).add(2) Array. The from (set) / / [1, 2] / / set to Array: extended operator var arr = set [...] / / [1, 2]Copy the code
- The Set of traverse
ForEach var set = new set ([1,3,'ha']) set.foreach ((item,idx)=>{console.log(item,idx)}) // 1 1/3 3 3 // 3 ha ha // The second value of a traditional forEach function is an index, but Set is a Set of key-value pairs, and the second argument represents the key, Keys returns a traversal for(let item of set.keys()){console.log(item)} // 3. values returns a traversal for(let item of) Set.values ()){// The key and value of set are equal, So keys and values return the same value console.log(item)} // 4. entries() returns the traverser for(let item of set.entries()){console.log(item) // [key,value]}Copy the code
Weak Set collection
methods
The add(), has(), and delete() methods are supported. Examples are as follows:
Var set = new WeakSet() var obj = {a:1} set.add(obj) set.has(obj) // true set.delete(obj) // true, Set :{} set.add(1) // TypeError: Invalid value used in weak setCopy the code
other
The Weak Set cannot be iterated and cannot be looped by for-of.
The Weak Set does not expose any iterators, so its contents cannot be detected by the program itself
The Week Set does not support the forEach method
The Week Set does not support the size attribute.
Weak Set seems to have limited functionality, but in fact it can properly handle data in memory.
conclusion
The Set and the Weak Set sets are used and why there are Weak sets.