Javascript 简明教程

JavaScript - The WeakSet Object

JavaScript WeakSet 对象是对象的集合。WeakSet 与 Set 非常相似。WeakSet 和 Set 之间的主要区别在于 WeakSet 只包含对象,而 Set 还可以包含数字、布尔值、字符串等值。

The JavaScript WeakSet object is a collection of objects. The WeakSet is very similar to the Set. The main difference between the WeakSet and Set is that the WeakSet contains only objects, and the Set can also contain the values like numbers, Boolean, string, etc.

WeakSet 对象是弱的,这意味着对 WeakSet 中对象的引用是弱的。如果没有其他对存储在 WeakSet 中的值的引用,则这些值会被回收。

WeakSet objects are weak, meaning that references to objects in a WeakSet are held weakly. If no other references to a value stored in the WeakSet exist, those values can be garbage collected.

WeakSet 对象可用于跟踪对象而不阻止它们被回收。一个例子是跟踪 DOM 节点而无需手动从 DOM 中删除它们。

WeakSet objects are useful for keeping track of objects without preventing them from being garbage collected. An example of this would be keeping track of DOM nodes without having to remove them from the DOM manually.

Syntax

按照以下语法在 JavaScript 中定义 WeakSet 类的新的实例。

Follow the syntax below to define a new instance of the WeakSet class in JavaScript.

const weakest = new WeakSet([iterable]);

我们在上述语法中使用 'new' 关键字使用了 WeakSet() 构造函数。

We used the WeakSet() constructor function with a 'new' keyword in the above syntax.

Parameters

  1. iterable − It is an iterable containing multiple objects to initialize the WeakSet.

在此,我们列出了 WeakSet 的方法和属性。

Here, we have listed the methods and properties of the WeakSet.

WeakSet Properties

以下是 WeakSet 的所有属性及其说明的列表。

Here is a list of all the properties of WeakSet and their description.

Sr.No.

Property & Description

1

constructor It returns the WeakSet constructor function.

WeakSet Methods

以下是与 WeakSet 对象相关的方法列表及说明。

Here is a list of the methods associated with WeakSet object and their description.

Sr.No.

Methods & Description

1

add() To insert the object into the WeakSet.

2

delete() To delete a single object from the WeakSet.

3

has() Check whether a particular object exists in the WeakSet.

Examples

Example: Similar Objects with WeakSet

在下面的示例中,我们定义了 empty 对象 obj1 和 obj2,并将它们添加到 WeakSet 中。

In the example below, we have defined the obj1 and obj2 empty objects and added them to the WeakSet.

此外,我们使用了 has() 方法来检查集合中是否包含 Obj1 和 obj2 对象。两个对象看起来相似,但具有内存中的不同引用。因此,WeakSet 包含这两个对象。

Also, we used the has() method to check whether the set contains the obj1 and obj2 objects. Both objects look similar but have different references in the memory. So, the WeakSet contains both objects.

<html>
<body>
   <p id = "output"> </p>
   <script>
      const output = document.getElementById("output");
      const obj1 = {};
      const obj2 = {};
      const weak_set = new WeakSet([obj1, obj2]);

      if(weak_set.has(obj1)) {
         output.innerHTML += "The weak_set contains the obj1 object! <br>";
      }

      if(weak_set.has(obj2)) {
         output.innerHTML += "The weak_set contains the obj2 object! <br>";
      }
   </script>
</body>
</html>
The weak_set contains the obj1 object!
The weak_set contains the obj2 object!

Example: Add Object in WeakSet

在下面的示例中,我们定义包含 0 个元素的 WeakSet。此外,我们已经定义了汽车对象,并使用 add() 方法将对象添加到集合中。

In the example below, we have defined the WeakSet containing 0 elements. Also, we have defined the car object and used the add() method to add the object in the set.

然后,我们使用 has() 方法来检查是否已将对象添加到 WeakSet 中。

After that, we use the has() method to check whether the object has been added into the WeakSet.

<html>
<body>
   <p id = "output"> </p>
   <script>
      const output = document.getElementById("output");
      const weak_set = new WeakSet();
      const car = {
         brand: "Audi",
         model: "Q5",
      }
      weak_set.add(car);

      if (weak_set.has(car)) {
         output.innerHTML = "The car object is added successfully to the weak_set.";
      }
   </script>
</body>
</html>

Output

The car object is added successfully to the weak_set.

Example: Delete Object from the WeakSet

在下面的示例中,我们使用汽车对象初始化了 WeakSet。然后,我们使用 delete() 方法从 WeakSet 中删除该对象。

In the example below, we have initialized the WeakSet with the car object. After that, we use the delete() method to delete the object from the WeakSet.

<html>
<body>
   <p id = "output"> </p>
   <script>
      const output = document.getElementById("output");
      const car = {
         brand: "Audi",
         model: "Q5",
      }
      const carWeakSet = new WeakSet([car]);
	   const flag = carWeakSet.delete(car); // returns true if deleted successfully
      output.innerHTML = "The car object is deleted successfully from the carWeakSet? " + flag;
   </script>
</body>
</html>

Output

The car object is deleted successfully from the carWeakSet? true