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


underscore.js _.isWeakSet()用法及代码示例


Underscore.js是一个JavaScript库,它使对数组,字符串,对象的操作更加容易和方便。
_.isWeakSet()函数用于检查给定对象是否为JavaScript弱集。链接underscore.js CDN时,“_”作为全局变量附加到浏览器。

用法:

_.isWeakSet( object );

参数:

  • object:它是任何JavaScript对象,例如数组,字符串,映射,集合等。

返回值:它返回布尔值。如果集合是弱集合,则返回true,否则返回false。

范例1:当给出一个弱集时,它返回true。



<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content= 
        "width=device-width, initial-scale=1.0"> 
  
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
  
        // Creating a weak set using 
        // constructor 
        var obj = new WeakSet(); 
  
        // Using the _.weakSet() function 
        var isWeakSet = _.isWeakSet(obj); 
        console.log(isWeakSet) 
  
        // If the given object is weakset  
        // it prints the object is weak set. 
        if (isWeakSet) 
            console.log(`The ${obj} is the  
                    WeakSet of Javascript.`) 
        else 
            console.log(`The ${obj} is not  
                the WeakSet of Javascript.`) 
    </script> 
</body> 
  
</html>

输出:

范例2:给定数组时,输出为false。

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content= 
        "width=device-width, initial-scale=1.0"> 
  
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
  
        // Creating a array of size  
        // 2 using constructor 
        var obj = new Array(2); 
  
        // Filling value 10in the array 
        obj.fill(10) 
  
        // Using the _.weakSet() function 
        var isWeakSet = _.isWeakSet(obj); 
        console.log(isWeakSet) 
  
        // If the given object is weakset  
        // it prints the object is weak set 
        if (isWeakSet) 
            console.log(`The ${obj} is the 
                WeakSet of Javascript.`) 
        else 
            console.log(`The ${obj} is not  
                the WeakSet of Javascript.`) 
    </script> 
</body> 
  
</html>

输出:




相关用法


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