问题:
Here is a piece of code I have in several places in my code:
req = this.getActiveRequestById(message.trans_id);
if (!req) {
break;
}
How can I rewrite this as a on...
可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:
问题:
Here is a piece of code I have in several places in my code:
req = this.getActiveRequestById(message.trans_id);
if (!req) {
break;
}
How can I rewrite this as a one-liner?
EDIT I want to clarify that I need to store the return value in req for later use.
回答1:
Remove var assignment and apply the condition directly to the value:
if (!this.getActiveRequestById(message.trans_id)) break;
I would not suggest it but if you need to set a variable in a one liner if condition it should work:
if (!(req = this.getActiveRequestById(message.trans_id))) break;
Notice that it generates confusion between comparison and assignment.
回答2:
if (!(reg = this.getActiveRequestById(message.trans_id))) break;