This is the 26th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Preface:

Review the previous article on the important data structure of JS JSON & Set understanding/use after processing data to interact, JSON is often used to deal with more. This paper is to record and learn another data structure: WeakSet

In the previous series of articles, we on some of the knowledge of JavaScript for a series of learning, but also through THE JS implementation of classical algorithms for specific learning, summary more reading -list & classic sorting algorithm -list

JavaScript data structure

ES6 new data structure: WeakSet is similar to the structure of the above literature **Set**, which is also a Set of non-repeating values.

But there are differences:

  1. WeakSet members can only be objects, not other types of values

  2. Objects in a WeakSet are weak references

    The garbage collection mechanism does not consider WeakSet’s reference to this object. Understanding: If the object is no longer referenced by other objects, the garbage collection mechanism will automatically reclaim the memory occupied by the object even if the object still exists in WeakSet

WeakSet()

WeakSet | MDN WeakSet object allows you to keep weak object is stored in a collection.

WeakSet() is also a constructor. Initialize a WeakSet data structure by new one

let ws = new WeakSet(a);Copy the code

WeakSet grammar:

 new WeakSet([iterable]);
Copy the code

WeakSet, as a constructor, can accept an array or array-like object as a parameter. (In fact, any object with an Iterable interface can also be used as a WeakSet parameter.) All members of the array automatically become members of a WeakSet instance object. Note that NULL is considered undefined

let arrarr = [[1.2], [3.4]].let ws = new WeakSet(arrarr);
// WeakSet {[1, 2], [3, 4]}
Copy the code

In the example above, the elements of the Arrarr array are objects

The following example array arR members are not objects, adding WeakSet will report an error.

let arr = [3.4];
let ws = new WeakSet(arr);
// Uncaught TypeError: Invalid value used in weak set(...)
Copy the code

WeakSet has no way to traverse its members because it also has no size property

Three methods of WeakSet structure:

  • Weakset.prototype. add(value) : Adds a new member to the WeakSet instance
  • Weakset.prototype. delete(value) : Clears the specified member of a WeakSet instance
  • Weakset.prototype. has(value) : Returns a Boolean value indicating whether a value is in a WeakSet instance
let ws = new WeakSet(a);let obj = {};
let foo = {};

ws.add(window);
ws.add(obj);

ws.has(window); // true
ws.has(foo);    // false

ws.delete(window);
ws.has(window);    // false
Copy the code

Read more:

Classical sorting algorithm:

  • 【 jS-sort algorithm -sort()】,
  • 【JavaScript- sort algorithm – Hill sort 】
  • 【JS- sorting algorithm – merge sort 】,
  • 【JavaScript- sorting algorithm – counting sort 】
  • 【JS- sort algorithm – bubble sort 】
  • JS- Classical sorting algorithm – Selection sort,
  • 【JS implementation – classical sorting algorithm – insert sort 】
  • JS implementation – classical sorting algorithm -JS implementation of radixSort (radixSort)
  • Learn the classic sort algorithm -JS to implement quickSort
  • Learn sorting algorithm skills – three – way fast sorting

JavaScript learning and Improvement

  • 】 【 Array. The prototype. The map (),
  • JS- special symbol – bit operator
  • 【ES6 – for/of】,
  • JS- Logical operator – short circuit? ,
  • 【JS- arrow function 】
  • 】 【 JavaScript – forEach (),
  • 【JS- Important data type -JSON】
  • Js-data structure -JSON- Processing using – Front and back end interactive transfer of data
  • 【 JS-data Structure-ES6-set 】