[英]Ensure all methods in a chained comparator in javascript are run before evaulating?
If I have the following snippet:如果我有以下代码段:
onClose={() => !isPostingConfig && onClickAway() && clearRoleErrors()}
I understand that if !isPostingConfig
returns a falsy
value, both onClickAway()
and clearRoleError()
will not be evaluated ie the methods will not run.我知道如果
!isPostingConfig
返回一个falsy
值,则不会评估onClickAway()
和clearRoleError()
,即这些方法将不会运行。
I know I can circumvent this by wrapping a method around this.我知道我可以通过包装一个方法来规避这个问题。
evaulate(){
var a = !isPostingConfig
var b = onClickAway()
var c = clearRoleError()
return a && b && c
}
But is there an easier to ensure all methods in a chained operator are run before evaluating the operators?但是,在评估运算符之前,是否更容易确保链式运算符中的所有方法都运行?
You can create an array of functions, and loop with Array.reduce()
.您可以创建一个函数数组,并使用
Array.reduce()
循环。 Combine the result of each function with the previous one using the logical and operator ( &&
).使用逻辑与运算符 (
&&
) 将每个 function 的结果与前一个结果组合。 All the functions will execute, and the end result stay true
if all return true
, or false
if at least one returns false
.所有函数都将执行,如果所有函数都返回
true
,则最终结果保持true
,如果至少有一个返回false
,则最终结果保持为false
。
const evaluate = (...args) => args.reduce((r, fn) => r && fn(), true)
and you can call it like this:你可以这样称呼它:
evaluate(() => !isPostingConfig, onClickAway, clearRoleError)
If you don't want to wrap all expressions with a function, you can add a typeof check:如果不想用 function 包装所有表达式,可以添加 typeof 检查:
const evaluate = (...args) => args.reduce((r, item) => r && (typeof item === 'function' ? item() : item), true)
and call it like this:并这样称呼它:
evaluate(!isPostingConfig, onClickAway, clearRoleError)
But is there an easier to ensure all methods in a chained operator are run before evaluating the operators?
但是,在评估运算符之前,是否更容易确保链式运算符中的所有方法都运行?
You can use &
instead of &&
assuming you're dealing with booleans (you are with !isPostingConfig
, but I don't know the return values of your functions).您可以使用
&
而不是&&
假设您正在处理布尔值(您使用的是!isPostingConfig
,但我不知道您的函数的返回值)。 &
is the bitwise AND operator. &
是按位与运算符。 It always evaluates both of its operands.它总是评估它的两个操作数。 Then it converts them to 32-bit integers and ANDs them together.
然后它将它们转换为 32 位整数并将它们与在一起。
false
converts to 0
and true
converts to 1
, so with boolean values, a & b & c
will be 1
if they're all 1
or 0
if any of them is 0
. false
转换为0
并且true
转换为1
,因此对于 boolean 值, a & b & c
将为1
如果它们都是1
或0
如果它们中的任何一个是0
。
Of course, the result will be a number, not a boolean, but you can convert it back:当然,结果将是一个数字,而不是 boolean,但您可以将其转换回来:
onClose={() => Boolean(!isPostingConfig & onClickAway() & clearRoleErrors())}
You might give yourself andAll
and orAll
helper functions:你可以给自己
andAll
和orAll
辅助函数:
onClose={() => andAll(!isPostingConfig, onClickAway(), clearRoleErrors())}
The array methods every
(= AND) and some
(= OR) would work, but it's a bit cumbersome:数组方法
every
(= AND) 和some
(= OR) 都可以,但有点麻烦:
onClose={() => [!isPostingConfig, onClickAway(), clearRoleErrors()].every(Boolean)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.