我有许多可变长度的函数数组,其中数组中的每个函数都采用相同类型的单个参数并返回 true 或 false。
如何将这些函数“组合”成一个函数来测试每个组件函数是否返回 true?
var less_than = function(y) {
function(x) {
return x < y;
}
}
var greater_than = function(y) {
function(x) {
return x > y;
}
}
var is_even = function(x) {
return x % 2 == 0;
}
var fns = [less_than(10), greater_than(1), is_even];
var test_function = combine_tests(fns);
test_function(8) // => true
最佳答案
The every() method tests whether all elements in the array pass the test implemented by the provided function.
var functions = [f1, f2, f3];
var arg = "foo";
var result = functions.every(function(func){
return func(arg);
});
关于javascript - 如何将 bool 函数数组组合成单个 bool 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35219469/