Javascript 简明教程

JavaScript - The WeakMap Object

JavaScript 中的 WeakMap 对象是一个键值对的集合,其中键是弱引用的。WeakMap 键必须是对象或未注册符号,且值是任何任意 JavaScript 类型。

A WeakMap object in JavaScript is a collection of key-value pairs where the keys are weakly referenced. The WeakMap keys must be objects or non-registered symbols, and values are of any arbitrary JavaScript type.

WeakMap 与 JavaScript Map 类似。WeakMap 和 Map 数据结构之间主要的区别在于 WeakMap 数据结构仅使用对象作为键,而 Map 也可将其他数据类型用作键。

The WeakMap is similar to the JavaScript Map. The main difference between the WeakMap and Map data structure is that the WeakMap data structure uses the objects as a key only, but Map can use other data types also as a key.

如果您将其他数据类型的值用作 WeakMap 的键(对象除外),则会引发类型错误。

If you use the value of another data type as a key of a WeakMap except the object, it gives the Types error.

Syntax

您可以按照以下语法在 JavaScript 中使用 WeakMap:

You can follow the syntax below to use the WeakMap in JavaScript −

const weak_map = new WeakMap();

在以上语法中,我们使用带有 WeakMap() 函数的“new”关键字创建 WeakMap 的新实例。

In the above syntax, we used the 'new' keyword with a WeakMap() function to create a new instance of the WeakMap.

WeakMap 提供了从 WeakMap 中设置、获取和删除 key-value 对的方法。在此,我们列出了 WeakMap 的属性和方法。

The WeakMap provides methods to set, get, and delete the key-value pairs from the WeakMap. Here, we have listed the properties and methods of the WeakMap.

WeakMap Properties

以下是对 WeakMap 属性及其说明的列表:

Here is a list of the properties of WeakMap and their description −

Sr.No.

Name & Description

1

constructor To get a constructor function of a WeakMap.

WeakMap Methods

以下是对 WeakMap 对象相关联的方法及其说明的列表:

Here is a list of the methods associated with WeakMap object and their description −

Sr.No.

Name & Description

1

delete() To delete a single key-value pair from the WeakMap.

2

get() To get values related to the specific object.

3

has() Check whether a particular object exists as a key exists in the WeakMap.

4

set() To insert the key-value pair in the WeakMap.

WeakMap Constructor()

以下是在 JavaScript 中使用 WeakMap 构造函数 -

Following is the WeakMap constructor in JavaScript −

Sr.No.

Name & Description

1

WeakMap() To create a WeakMap object.

Examples

Example: Inserting key-value pair to the WeakMap

在下面的示例中,我们使用构造函数来定义 WeakMap。然后,我们使用 set() 方法将 laptop 对象设置为键,并将它的价格设置为值。

In the example below, we have defined the WeakMap using the constructor function. After that, we used the set() method to set the laptop object as a key and its price as a value.

最后,我们使用 get() 方法来获取与 "laptop" 键相关的键值。

At last, we used the get() method to get the value related to the 'laptop' key.

<html>
<body>
   <p id = "output">The laptop price is: </p>
   <script>
      const wm = new WeakMap();
      const laptop = {
         brand: "HP",
         model: "Pavilion",
      }
      wm.set(laptop, 100000);
      document.getElementById("output").innerHTML += wm.get(laptop);
   </script>
</body>
</html>
The laptop price is: 100000

如果我们执行这个程序,它会返回与 "laptop" 键相关的键值,即 "10000"。

If we execute the program, it returns the value related to the "laptop" key, i.e. "10000".

Example: Deleting key-value pair from the WeakMap

在下面的示例中,我们使用 set() 方法向 WeakMap 中插入了键值对。

In the example below, we have inserted the key-value pair in the WeakMap using the set() method.

然后,我们使用 delete() 方法从 WeakMap 中删除了键值对。在删除键值对然后访问它时,WeakMap 会返回未定义,如你可以在输出中看到的那样。

After that, we used the delete() method to delete the key-value pair from the WeakMap. After deleting the key-value pair when you access it, the WeakMap returns undefined, as you can see in the output.

Note − WeakMap 在 JavaScript 中不可迭代。

Note − WeakMaps are not iterable in JavaScript.

<html>
<body>
   <div id = "output1">The laptop price is:  </div>
   <div id = "output2">The laptop price after deletion is:  </div>
   <script>
      const wm = new WeakMap();
      const laptop = {
         brand: "HP",
         model: "Pavilion",
      }
      wm.set(laptop, 100000);
      document.getElementById("output1").innerHTML += wm.get(laptop);

      wm.delete(laptop);
      document.getElementById("output2").innerHTML += wm.get(laptop);
   </script>
</body>
</html>
The laptop price is: 100000
The laptop price after deletion is: undefined

因为它从 WeakMap 中删除了键值,所以它会返回 "undefined"。

It returns "undefined" as the key-value is deleted from the WeakMap.