根据条件调用参数amount的函数



我有一个名为" func "的函数:

const otherFunc = (arg1, arg2) => {...}
const func = (condition1, condition2) => {
condition1 || condition2 ? otherFunc(value, true) : otherFunc(false)
}

以前的方式工作,但我想知道是否有一种方法来避免使用两个不同的调用otherFunc。我尝试了这个,但语法不正确:

const func = (condition1, condition2) => {
otherFunc((condition1 || condition2) && ...[value, true])
}

编辑:这是一个连接if和else的简单函数:

function is(value, done) {
return {
else: (v) => is(done ? value : v, done),
if: (condition) => (done || condition ? is(value, true) : is(null)),
get: value,
};
}
const n = 50000;
console.log(
is('small')
.if(n < 1000) 
.else('big')
.if(n > 10000)
.else('medium-big')
.if(n > 5000)
.else('medium-small').get,
);

如果您可以将参数放入数组中,那么您可以在数组之间交替使用以扩展到参数列表中。

otherFunc(
...((condition1 || condition2) ? [value, true] : [false])
);

也就是说,它不是很好读。我真的更喜欢

if (condition1 || condition2) {
otherFunc(value, true);
} else {
otherFunc(false);
}

好可维护的代码不是高尔夫比赛——一样干绝对可能并不总是最好的方法。

最新更新