在javascript (或类型记录)中是否有一种方法可以避免在if语句条件下重写同一对象两次?
就像这样:
if (A != null || A != B) {
// do something here
}
// something in the form of this:
if (A != null || != B) { // avoid re-writing "A" here
// do something here
}有人对此有任何暗示甚至其他问题吗?
发布于 2022-10-19 09:51:26
你可以这样做:
if([B, null].includes(A)) {
// ...
}发布于 2022-10-19 09:50:51
在if条件下没有内置的快捷方式。实际上,如果A是一个您希望避免重写的繁琐表达式,通常的解决方案将是一个临时变量:
if ( (t=A) != null || t != B ) {https://stackoverflow.com/questions/74123245
复制相似问题