JavaScript的weakSet.delete()方法用法介绍

2021年5月2日18:27:36 发表评论 808 次浏览

以下是示例weakSet.add()方法。

<script> function gfg() { const A = new WeakSet(); const B = {}; A. delete (B); document.write(A.has(B) + "<br>" ); } gfg(); </script>

输出如下:false

weakSet.delete()是JavaScript中的内置函数, 用于从列表中删除特定元素弱集目的。

语法如下:

weakSet.delete(A);

参数:它接受参数" A", 该参数是将从弱集对象中删除的值。

返回值:如果已成功从弱集对象中删除了元素, 则返回true;如果尚未成功删除该元素或在弱集中找不到该元素, 则返回false。

JavaScript代码显示此功能的工作方式:

代码1:

<script>
  
    //Constructing weakSet() object
    const A = new WeakSet();
  
    //Creating a new element
    const B = {};
  
    //Adding the element to the weakset object
    A.add(B);
  
    //Testing whether the element has been
    //set into the weakset object or not
    document.write(A.has(B) + "<br>" );
  
    //Deleting B form the weakSet() object
    A. delete (B);
  
    //Testing whether the element "B" has been deleted or not
    document.write(A.has(B) + "<br>" );
  
</script>

输出如下:

true
false

此处的输出最初为true, 表示元素" B"已成功设置到weakSet对象中, 之后为false表示元素" B"已成功从weakSet对象中删除。

支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • 苹果Safari
  • 歌剧

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: