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


underscore.js _.isNaN()用法及代碼示例

_.isNaN() 函數:

  • 它用於查找傳遞的對象的值是否為 NaN。
  • 如果對象的值為 NaN,則輸出為真,否則為假。
  • 我們甚至可以在這個函數中執行加法、減法等操作。

用法:

_.isNaN(object)

參數:
它隻需要一個參數,即需要檢查的對象。
返回值:
如果對象的值為 NaN,則返回 true,否則返回 false。
Examples:

  • 將數字傳遞給 _.isNan() 函數:
    _.isNaN() 函數獲取傳遞給它的元素並檢查它的值是否為 NaN。由於傳遞了一個數字,並且我們知道該數字有自己的值,因此輸出將為 false。

html


<!-- Write HTML code here -->
<html>
  
<head>
    <script src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
    </script>
</head>
  
<body>
    <script type="text/javascript">
        var a=10;
        console.log(_.isNaN(10));
    </script>
</body>
  
</html>

輸出:



  • 將“NaN”傳遞給 _.isNan() 函數:
    由於這一次,Nan 本身被傳遞給函數,因此,當函數檢查時,它發現傳遞的變量具有 NaN 值。因此,輸出將是真實的。

html


<!-- Write HTML code here -->
<html>
   
<head>
    <script src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
    </script>
</head>
   
<body>
    <script type="text/javascript">
        console.log(_.isNaN(NaN));
    </script>
</body>
   
</html>

輸出:

  • 將 “undefined” 傳遞給 _.isNaN() 函數:
    _.isNaN() 函數獲取參數,這裏是 “undefined” 並開始檢查。我們知道,“Undefined” 沒有任何值,因此它的值絕對不是 NaN。因此,答案是錯誤的。

html


<!-- Write HTML code here -->
<html>
   
<head>
    <script src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
    </script>
</head>
   
<body>
    <script type="text/javascript">
        console.log(_.isNaN(undefined));
    </script>
</body>
   
</html>

輸出:

  • 對_.isNan()函數的輸出執行操作:
    在這裏,我們使用上麵解釋的示例 2 和 3。然後將它們的值存儲在變量 ‘a’ 和 ‘b’ 中。因此,變量 ‘a’ 為假,‘b’ 為真。最後,我們對 ‘a’ 和 ‘b’ 執行 OR 運算,並將結果存儲在變量 ‘c’ 中。由於 ‘b’ 為真,因此,‘c’ 為 1。

html


<!-- Write HTML code here -->
<html>
   
<head>
    <script src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
    </script>
</head>
   
<body>
    <script type="text/javascript">
        var a = _.isNaN(undefined);
        var b = _.isNaN(NaN);
        var c = a + b;
        console.log(a);
        console.log(b);
        console.log(c);
    </script>
</body>
   
</html>

輸出:

筆記:
這些命令在 Google 控製台或 Firefox 中不起作用,因為需要添加他們沒有添加的這些附加文件。
因此,將給定的鏈接添加到您的 HTML 文件中,然後運行它們。
鏈接如下:

html


<!-- Write HTML code here -->
<script type="text/javascript" src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>



相關用法


注:本文由純淨天空篩選整理自Sakshi98大神的英文原創作品 Underscore.js _.isNaN() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。