JavaScript - WeakSet delete() 方法



JavaScript WeakSet 的 delete() 方法用于从当前 WeakSet 中移除指定的元素或对象。当 add() 方法将新对象添加到 WeakSet 时,delete() 方法可以移除相同的元素。如果成功从 WeakSet 中移除指定的元素,则返回布尔值 true,否则返回 false

JavaScript 中的 WeakSet 是一个集合,它只能包含对象和未注册的符号。与其他集合(如 Set)不同,它不能存储任何类型的任意值。

语法

以下是 JavaScript String delete() 方法的语法:

weakSetInstance.delete(value)

参数

此方法接受一个名为“value”的参数,如下所述:

  • value - 要从 WeakSet 对象中移除的值。

返回值

如果元素已成功从 WeakSet 对象中移除,则此方法返回 true,否则返回 false。

示例

示例 1

如果传递的值或对象存在于此 WeakSet 中,它将从 WeakSet 中移除该对象并返回true

在下面的示例中,我们使用 JavaScript WeakSet delete() 方法通过 add() 方法从 WeakSet 对象中移除附加的新对象(一个空对象 {})。

<html>
<head>
   <title>JavaScript WeakSet delete() Method</title>
</head>
<body>
   <script>
      const Obj = new WeakSet();
      const newObj = {};
      document.write("WeakSet.add(newObj): ", Obj.add(newObj));
      document.write("<br>------Before remove-----");
      document.write("<br>Does WeakSet have an object? ", Obj.has(newObj));
      document.write("<br>Is the object removed from this WeakSet? ", Obj.delete(newObj));
      document.write("<br>------After remove-----");
      document.write("<br>Does WeakSet have an object? ", Obj.has(newObj));
   </script>    
</body>
</html>

输出

上述程序返回以下语句:

WeakSet.add(newObj): [object WeakSet]
------Before remove-----
Does WeakSet have an object? true
Is the object removed from this WeakSet? true
------After remove-----
Does WeakSet have an object? false

示例 2

如果在 WeakSet 中找不到传递的值,则返回false

在这个例子中,我们使用 JavaScript WeakSet delete() 方法来移除元素10。但是,由于 10 是无效值,因此不会将其添加到 WeakSet。delete() 方法将返回false,因为该元素不存在于 WeakSet 中。

<html>
<head>
   <title>JavaScript WeakSet add() Method</title>
</head>
<body>
   <script>
      const Obj = new WeakSet();
      const num = 10;
      document.write("Value: ", num);
      
      try {
         document.write("WeakSet.add(newObj): ", Obj.add(num));
      } catch (error) {
         document.write("<br>", error);
      }
      document.write("<br>------Before remove-----");
      document.write("<br>Does WeakSet have an object? ", Obj.has(num));
      document.write("<br>Is the object removed from this WeakSet? ", Obj.delete(num));
      document.write("<br>------After remove-----");
      document.write("<br>Does WeakSet have an object? ", Obj.has(num));
   </script>    
</body>
</html>

输出

执行上述程序后,它将返回“false”。

Value: 10
TypeError: Invalid value used in weak set
------Before remove-----
Does WeakSet have an object? false
Is the object removed from this WeakSet? false
------After remove-----
Does WeakSet have an object? false

示例 3

除了自定义对象外,我们还可以使用 add()delete() 方法向 WeakSet 对象添加和删除全局对象,例如'window'

<html>
<head>
   <title>JavaScript WeakSet add() Method</title>
</head>
<body>
   <script>
      const Obj = new WeakSet();
      document.write("The method WeakSet.add(window) returns: ", Obj.add(window));
      document.write("<br>----Before remove----");
      document.write("<br>Does WeakSet has 'window' object? ", Obj.has(window));
      document.write("<br>Is the 'window' object removed from this WeakSet? ", Obj.delete(window));
      document.write("<br>----After remove----");
      document.write("<br>Does WeakSet has 'window' object? ", Obj.has(window));
   </script>    
</body>
</html>

输出

执行上述程序后,它将返回以下语句:

The method WeakSet.add(window) returns: [object WeakSet]
----Before remove----
Does WeakSet has 'window' object? true
Is the 'window' object removed from this WeakSet? true
----After remove----
Does WeakSet has 'window' object? false
广告