问题描述
这是我在代码中多个地方编写的代码:
req = this.getActiveRequestById(message.trans_id);
if (!req) {
break;
}
如何将其重写为单线?
编辑我想澄清一下,我需要将返回值存储在req中以供以后使用。
1楼
删除var赋值并将条件直接应用于值:
if (!this.getActiveRequestById(message.trans_id)) break;
我不建议这样做,但是如果您需要在一个衬里中设置一个变量(如果条件可以的话):
if (!(req = this.getActiveRequestById(message.trans_id))) break;
请注意,这会在比较和分配之间产生混淆。
2楼
if (!(reg = this.getActiveRequestById(message.trans_id))) break;