如何在 switch 语句中使用 instanceof [英] How to use instanceof in a switch statement
问题描述
我使用自定义错误(es6-error)允许我处理基于他们的班级是这样的:
import { DatabaseEntryNotFoundError, NotAllowedError } from 'customError';函数 fooRoute(req, res) {doSomethingAsync().then(() => {//解决/成功返回 res.send(200);}).catch((错误) => {//拒绝/失败如果(DatabaseEntryNotFoundError 的错误实例){返回 res.send(404);} else if (error instanceof NotAllowedError) {返回 res.send(400);}log('由于未指定的错误执行异步操作失败:', error);返回 res.send(500);};}
现在我更愿意为这种类型的流使用开关,结果如下:
import { DatabaseEntryNotFoundError, NotAllowedError } from 'customError';函数 fooRoute(req, res) {doSomethingAsync().then(() => {//解决/成功返回 res.send(200);}).catch((错误) => {//拒绝/失败开关(错误实例){案例 NotAllowedError:返回 res.send(400);案例 DatabaseEntryNotFoundError:返回 res.send(404);默认:log('由于未指定的错误执行异步操作失败:', error);返回 res.send(500);}});}
instanceof 不像然而.所以后者失败了.
有没有办法在 switch 语句中检查实例的类?
一个不错的选择是使用 constructor
property 对象:
//拒绝/失败开关(错误.构造函数){案例 NotAllowedError:返回 res.send(400);案例 DatabaseEntryNotFoundError:返回 res.send(404);默认:log('由于未指定的错误执行异步操作失败:', error);返回 res.send(500);}
注意 constructor
必须与创建的对象完全匹配(假设 error
是 NotAllowedError
和 的实例NotAllowedError
是 Error
) 的子类:
error.constructor === NotAllowedError
是true
error.constructor === Error
是false
这与 instanceof
不同,它也可以匹配超类:
NotAllowedError 的错误实例
是true
error instanceof Error
是true
查看这篇有趣的帖子,了解constructor
属性.>
I use custom errors (es6-error) allowing me to handle errors based on their class like so:
import { DatabaseEntryNotFoundError, NotAllowedError } from 'customError';
function fooRoute(req, res) {
doSomethingAsync()
.then(() => {
// on resolve / success
return res.send(200);
})
.catch((error) => {
// on reject / failure
if (error instanceof DatabaseEntryNotFoundError) {
return res.send(404);
} else if (error instanceof NotAllowedError) {
return res.send(400);
}
log('Failed to do something async with an unspecified error: ', error);
return res.send(500);
};
}
Now I'd rather use a switch for this type of flow, resulting in something like:
import { DatabaseEntryNotFoundError, NotAllowedError } from 'customError';
function fooRoute(req, res) {
doSomethingAsync()
.then(() => {
// on resolve / success
return res.send(200);
})
.catch((error) => {
// on reject / failure
switch (error instanceof) {
case NotAllowedError:
return res.send(400);
case DatabaseEntryNotFoundError:
return res.send(404);
default:
log('Failed to do something async with an unspecified error: ', error);
return res.send(500);
}
});
}
instanceof doesn't work like that however. So the latter fails.
Is there any way to check an instance for its class in a switch statement?
A good option is to use the constructor
property of the object:
// on reject / failure
switch (error.constructor) {
case NotAllowedError:
return res.send(400);
case DatabaseEntryNotFoundError:
return res.send(404);
default:
log('Failed to do something async with an unspecified error: ', error);
return res.send(500);
}
Notice that the constructor
must match exactly with the one that object was created (suppose error
is an instance of NotAllowedError
and NotAllowedError
is a subclass of Error
):
error.constructor === NotAllowedError
istrue
error.constructor === Error
isfalse
This makes a difference from instanceof
, which can match also the super class:
error instanceof NotAllowedError
istrue
error instanceof Error
istrue
Check this interesting post about constructor
property.
这篇关于如何在 switch 语句中使用 instanceof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!