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


Underscore.js _.isMap()用法及代码示例


Underscore.js是javascript中的一个库,使对数组,字符串,对象的操作变得更加轻松便捷。 _.isMap()函数用于检查给定的对象是否为javascript Map。

注意:在浏览器中使用下划线函数之前,非常有必要链接下划线CDN。链接underscore.js CDN时“_”作为全局变量附加到浏览器。

用法:

_.isMap(object);

Parameters: 

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

返回值:它返回布尔值。如果对象是javascript的Map,则该函数返回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 array with value 10 
    obj.fill(10); 
    //using the underscore.js function _.isMap() 
    var isMap= _.isMap(obj); 
    console.log(isMap) 
    //If the given object is Map it prints the object is Map. 
    if(isMap) 
    console.log(`The ${obj} is the Map of Javascript.`) 
    else 
    console.log(`The ${obj} is not the Map of Javascript.`) 
  </script> 
</body> 
</html>

输出:

范例2:

给出Map后,它将返回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 Map using constructor 
    var obj= new Map(); 
    //using the underscore.js function _.isMap() 
    var isMap= _.isMap(obj); 
    console.log(isMap) 
    //If the given object is Map it prints the object is Map. 
    if(isMap) 
    console.log(`The ${obj} is the Map of Javascript.`) 
    else 
    console.log(`The ${obj} is not the Map of Javascript.`) 
  </script> 
</body> 
</html>

输出:

范例3:

给对象作为参数时,输出为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 Javascript Object using constructor 
    var obj= new Object(); 
    obj={ 
      "a":1, 
      "b":2 
    } 
    //using the underscore.js function _.isMap() 
    var isMap= _.isMap(obj); 
    console.log(isMap) 
    //If the given object is Map it prints the object is Map. 
    if(isMap) 
    console.log(`The ${obj} is the Map of Javascript.`) 
    else 
    console.log(`The ${obj} is not the Map of Javascript.`) 
  </script> 
</body> 
</html>

输出:

相关用法


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