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


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

Underscore.js是javascript中的一个库,使对数组,字符串,对象的操作变得更加轻松便捷。 _.isWeakMap()函数用于检查给定对象是否为javascriptweakmap。链接underscore.js CDN时“_”作为全局变量附加到浏览器。

用法:

_.isWeakMap(object);

Parameters: 

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

返回值:它返回布尔值。如果对象是JavaScript的弱映射,则该函数返回true,否则返回false。



为了更好地理解该函数,下面给出一些示例。

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


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charMap="UTF-8">
  <meta name="viewport" 
        content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <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(5)
    //using the underscore.js function _.weakMap()
    var isWeakMap= _.isWeakMap(obj);
    console.log(isWeakMap)
    //If the given object is weakMap it prints the object is weak Map.
    if(isWeakMap)
    console.log(`The ${obj} is the WeakMap of Javascript.`)
    else
    console.log(`The ${obj} is not the WeakMap of Javascript.`)
  </script>
</body>
</html>

输出:

范例2:

当给出一个弱集时,它返回true。


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charMap="UTF-8">
  <meta name="viewport" 
        content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <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 WeakMap();
    //using the underscore.js function _.weakMap()
    var isWeakMap= _.isWeakMap(obj);
    console.log(isWeakMap)
    //If the given object is weakMap it prints the object is weak Map.
    if(isWeakMap)
    console.log(`The ${obj} is the WeakMap of Javascript.`)
    else
    console.log(`The ${obj} is not the WeakMap of Javascript.`)
  </script>
</body>
</html>

输出:




相关用法


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