Reference: Ruan Yifeng tutorial

Set

  • Basic usage
  1. Similar to an array, but the values of the members are unique and there are no duplicate values.
  2. var s = new Set()
  3. 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
  1. Set. The prototype. The constructor: constructor, the default is Set function
  2. Set.prototype.size: Returns the number of Set instance members
  • Instance operation method
  1. Add (): Adds a value and returns the Set deconstruction itself
  2. Delete (): Deletes a value and returns a Boolean value indicating whether the deletion was successful
  3. Has (): Returns a Boolean value indicating whether the argument is a member of the Set structure
  4. Clear: Clears all members with no return value
  • The traversal method of an instance
  1. keys()
  2. Values () // The key name and key value of the Set structure are the same value
  3. entries()
  4. forEach()
  • conclusion
  1. 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
  1. WeakSet structure is similar to Set, which is also a collection of non-repeating values. However, it differs from Set in two ways
  2. WeakSet members can only be objects
  3. 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.
  4. var ws = new WeakSet()
  • Instance methods
  1. WeakSet.prototype.add()
  2. WeakSet.prototype.delete()
  3. WeakSet.prototype.has()

Map

  • Basic usage
  1. 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.
  2. You can take an array whose members are arrays of keysnew Map([['name', 'alex.cheng'], ['age', 24]])
  3. Get an unknown key, return undefined
  4. Map keys are actually bound to memory addresses, and as long as the memory addresses are different, they are treated as two keys
  5. 0 and -0, NaN and NaN represent the same key
  • Instance properties and methods
  1. Map.prototype.size
  2. Map.prototype.constructor
  3. set(key, value)
  4. get(key)
  5. has(key)
  6. delete(key)
  7. clear()
  • Traversal methods
  1. keys()
  2. values()
  3. entries()
  4. forEach()
  • conclusion
  1. 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

  1. 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

  1. Helps prevent memory leaks