Underscore.js 是 javascript 中的一個庫,它使對數組、字符串、對象的操作更加容易和方便。 _.isError() 函數用於檢查給定對象是否為javascript錯誤對象。
注意:在瀏覽器中使用下劃線函數之前,非常有必要鏈接下劃線CDN。鏈接underscore.js CDN時“_”作為全局變量附加到瀏覽器。
用法:
_.isError(object);
參數:它隻需要一個參數,即對象。
返回:它返回布爾值。如果該對象是繼承自 javascript 的 Error 對象,則返回 true,否則該函數返回 false。
為了更好地理解該函數,下麵給出一些示例。
範例1:從錯誤對象創建錯誤時
<!DOCTYPE html>
<html>
<head>
<script src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
</script>
</head>
<body>
<script>
let e=new Error()
let ans=_.isError(e)
console.log(_.isError(e))
if(ans)
console.log("It is the javascript error")
</script>
</body>
</html>
輸出:
範例2:當 try catch 用於拋出錯誤時。
<!DOCTYPE html>
<html>
<head>
<script src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
</script>
</head>
<body>
<script>
let e;
try{
alrt("GeeksforGeeks")
}
catch(e){
let ans=_.isError(e)
console.log(`Error is ${e}`)
console.log(_.isError(e))
if(ans)
console.log("It is the javascript error object")
}
</script>
</body>
</html>
輸出:
範例3:當給定錯誤是字符串類型時。
<!DOCTYPE html>
<html>
<head>
<script src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
</script>
</head>
<body>
<script>
let e="some error"
let ans=_.isError(e)
console.log(_.isError(e))
if(ans)
console.log("It is the javascript error object")
else
console.log("It is not the javascript error object")
</script>
</body>
</html>
輸出:
相關用法
- Lodash _.isError()用法及代碼示例
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP array_udiff_uassoc()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- Tensorflow.js tf.layers.embedding()用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 Underscore.js _.isError() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。