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>
輸出:
相關用法
- Node.js util.types.isWeakSet()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- p5.js nfp()用法及代碼示例
- p5.js nfs()用法及代碼示例
- p5.js min()用法及代碼示例
- p5.js sq()用法及代碼示例
- d3.js d3.set.has()用法及代碼示例
- d3.js d3.set.add()用法及代碼示例
- PHP pi( )用法及代碼示例
- p5.js nfc()用法及代碼示例
- PHP dir()用法及代碼示例
- p5.js abs()用法及代碼示例
- p5.js box()用法及代碼示例
- p5.js cos()用法及代碼示例
- d3.js d3.lab()用法及代碼示例
- p5.js nf()用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 Underscore.js _.isWeakSet() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。