當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。