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

2021年5月2日18:24:58 发表评论 775 次浏览

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

<script> function gfg() { const A = new WeakSet(); const object = {}; A.add(object); document.write(A.has(object)); } gfg(); </script>

输出如下: true

weakSet.has()是JavaScript中的内置函数, 用于返回布尔值, 该布尔值指示对象中是否存在对象。弱集或不。通过WeakSet对象, 可以将弱保存的对象存储在集合中。

语法如下:

weakSet.has(A);

参数:它接受参数" A", 这是一个要检查其是否存在于weakSet对象中的值。

返回值:如果存在该元素, 则返回布尔值true, 否则返回false。

例子:

Input: weakset.has(object1); 
Output: true

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

代码1:

<script>
  
    //Constructing a weakSet() object
    const A = new WeakSet();
  
    //Constructing new objects
    const object1 = {};
  
    //Adding the new object to the weakSet
    A.add(object1);
  
    //Checking whether the new object is present
    //in the weakSet or not
    document.write(A.has(object1) + "<br>" );
  
</script>

输出如下:

true

这里的输出为true, 因为WeakSet()对象中存在对象" object1"。

代码2:

<script>
  
    //Constructing a weakSet() object
    const A = new WeakSet();
  
    //Constructing new objects
    const object1 = {};
  
    //Checking whether the new object is present
    //in the weakSet or not
    document.write(A.has(object1) + "<br>" );
  
</script>

输出如下:

false

这里的输出为false, 因为WeakSet()对象中不存在对象" object1"。

支持的浏览器:

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

木子山

发表评论

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