Returns true if both functions return true for a given set of arguments, false otherwise.
Use the logical and (&&) operator on the result of calling the two functions with the supplied args.
const both = (f, g) => (...args) => f(...args) && g(...args);
const isEven = num => num % 2 === 0;
const isPositive = num => num > 0;
const isPositiveEven = both(isEven, isPositive);
isPositiveEven(4); // true
isPositiveEven(-2); // false
我們設定兩個function,設定兩個不同條件一個是大於零 另外一個是設定2的倍數,可把兩個條件的function 合併在一起
const both = (f, g) => (...args) => f(...args) && g(...args);
裡面的 (...args) 就是運用了展開運算子 把陣列展開,傳入函式之中
function sum(a, b, c) {
return a + b + c;
}
var args = [1, 2, 3];
console.log(sum(...args)); // 6