当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaScript weakSet has()用法及代码示例


JavaScript弱集.has()方法用于返回一个布尔值,指示对象是否存在于弱集或不。 WeakSet 对象允许您将弱持有对象存储在集合中。

用法:

weakSet.has(value);

参数:此方法接受单个参数值。

  • value:将检查该值是否存在于weakSet对象中。

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

以下是weakSet.has()方法的示例:

示例 1:

javascript


function gfg() { 
    const A = new WeakSet(); 
    const object = {}; 
    A.add(object); 
    console.log(A.has(object)); 
} 
gfg();

输出:这里的输出为 true,因为对象 “object” 存在于 WeakSet() 对象中。

true

示例 2:

javascript


// 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 
console.log(A.has(object1));

输出:这里的输出为 true,因为对象 “object1” 存在于 WeakSet() 对象中。

true

示例 3:

javascript


// 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 
console.log(A.has(object1));

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

false

支持的浏览器:

  • 谷歌浏览器 36 及以上版本
  • 火狐浏览器 34 及以上版本
  • 苹果 Safari 9 及以上版本
  • Opera 23 及以上
  • 边 12 及以上

我们有完整的 JavascriptweakSet 方法列表,要检查这些方法,请阅读这篇JavaScript WeakSet Complete Reference 文章。



相关用法


注:本文由纯净天空筛选整理自ShivamKD大神的英文原创作品 JavaScript weakSet has() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。