Reference: Ruan Yifeng tutorial
Set
- Basic usage
- Similar to an array, but the values of the members are unique and there are no duplicate values.
- var s = new Set()
- The algorithm used inside a Set to determine whether two values are different is similar to the exact equality operator (‘===’). This means that two objects are always unequal. The only exception is NaN equals itself (‘===’ thinks NaN is not equal to itself)
- Instance properties
- Set. The prototype. The constructor: constructor, the default is Set function
- Set.prototype.size: Returns the number of Set instance members
- Instance operation method
- Add (): Adds a value and returns the Set deconstruction itself
- Delete (): Deletes a value and returns a Boolean value indicating whether the deletion was successful
- Has (): Returns a Boolean value indicating whether the argument is a member of the Set structure
- Clear: Clears all members with no return value
- The traversal method of an instance
- keys()
- Values () // The key name and key value of the Set structure are the same value
- entries()
- forEach()
- conclusion
- If you want to change the old Set structure synchronously during traversal, there is no direct way, only two workarounds.
/ / methodlet set= new Set (4-trichlorobenzene [1])set= new Set([... Set].map(item => item * 2)let set= new Set (4-trichlorobenzene [1])set = new Set(Array.from(set, x => x * 2))
Copy the code
WeakSet
- Basic usage
- WeakSet structure is similar to Set, which is also a collection of non-repeating values. However, it differs from Set in two ways
- WeakSet members can only be objects
- WeakSet objects are weak references, that is, garbage collection mechanism does not consider WeakSet’s reference to the object. That is, if the object is no longer referenced by other objects, the garbage collection mechanism will automatically reclaim the memory occupied by the object, regardless of whether the object still exists in the WeakSet structure.
- var ws = new WeakSet()
- Instance methods
- WeakSet.prototype.add()
- WeakSet.prototype.delete()
- WeakSet.prototype.has()
Map
- Basic usage
- Similar to objects, it is a collection of key-value pairs, but the range of ‘keys’ is not limited to strings (ordinary objects can only use strings as key values), and all types of values (including objects) can be used as keys.
- You can take an array whose members are arrays of keys
new Map([['name', 'alex.cheng'], ['age', 24]])
- Get an unknown key, return undefined
- Map keys are actually bound to memory addresses, and as long as the memory addresses are different, they are treated as two keys
- 0 and -0, NaN and NaN represent the same key
- Instance properties and methods
- Map.prototype.size
- Map.prototype.constructor
- set(key, value)
- get(key)
- has(key)
- delete(key)
- clear()
- Traversal methods
- keys()
- values()
- entries()
- forEach()
- conclusion
- Map does not have the Map and filter methods. The Map method is combined with the array method to implement filtering
let map1= new Map()
.set(1, 'a')
.set(2, 'b')
.set(3, 'c')
letMap1 map2 = new Map ([...] the filter (((key, value)) = > < 3 k)) / / filter (((key, value) = > {}) (key, value) here is to deconstruct the assignmentCopy the code
WeakMap
- The basic use
- WeakMap structure is basically similar to Map structure, except that it only accepts objects as key names (except null) and does not accept other types of values as key names, and the object to which the key name points is not counted in the garbage collection mechanism
- Helps prevent memory leaks