在JS中,似乎无法判断传递给函数的参数实际上是"error"类型还是error实例.
例如,这是无效的:
typeof err === 'error'
因为只有6种可能的类型(以字符串的形式):
typeof运算符以字符串形式返回类型信息.typeof
返回六个可能的值:
"数字"、"字符串"、"布尔"、"对象"、"函数"和"未定义".
但如果我有这样一个简单的用例呢:
function errorHandler(err) {
if (typeof err === 'error') {
throw err;
}
else {
console.error('Unexpectedly, no error was passed to error handler. But here is the message:',err);
}
}
那么,确定参数是否为错误实例的最佳方式是什么呢?
instanceof
号接线员有什么帮助吗?