_.isNaN()函數:
- 用於查找傳遞的對象的值是否為NaN。
- 如果對象的值為NaN,則輸出為true,否則為false。
- 我們甚至可以在此函數中執行加法,減法等操作。
用法:
_.isNaN(object)
參數:
它僅接受一個參數,這是需要檢查的對象。
返回值:
如果對象的值為NaN,則返回true;否則,返回false。
例子:
- 將數字傳遞給_.isNan()函數:
_.isNaN()函數采用傳遞給它的元素,並檢查其值是否為NaN。由於傳遞了數字,並且我們知道該數字具有自己的值,因此輸出將為false。<!-- 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值。因此,輸出將為真。<!-- 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。因此,答案是錯誤的。<!-- 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”執行“或”運算,並將結果存儲在變量“ c”中。由於“ b”為真,因此“ c”為1。<!-- 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文件,然後運行它們。
鏈接如下:
<!-- Write HTML code here -->
<script type="text/javascript" src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
相關用法
- Javascript Number.isNaN()用法及代碼示例
- underscore.js map()用法及代碼示例
- JQuery on()用法及代碼示例
- underscore.js every()用法及代碼示例
- underscore.js min()用法及代碼示例
- underscore.js max()用法及代碼示例
- JQuery first()用法及代碼示例
- underscore.js where()用法及代碼示例
- underscore.js contains()用法及代碼示例
- JQuery val()用法及代碼示例
- JQuery after()用法及代碼示例
- JQuery has()用法及代碼示例
- JQuery one()用法及代碼示例
- JQuery eq()用法及代碼示例
- JQuery last()用法及代碼示例
注:本文由純淨天空篩選整理自Sakshi98大神的英文原創作品 Underscore.js | _.isNaN() with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。