主题
_.cond(pairs)
_.cond() 方法创建一个谓词函数列表,并使用第一个返回真值的函数来产生其结果。
参数
pairs(Array): 谓词函数列表。每个谓词函数应该返回一个布尔值。
返回值
(Function): 返回新的复合函数。
示例
javascript
const func = _.cond([
[_.matches({ a: 1 }), () => "matches A"],
[_.conforms({ b: _.isNumber }), () => "matches B"],
[_.stubTrue, () => "no match"],
]);
console.log(func({ a: 1, b: 2 }));
// => 'matches A'
console.log(func({ a: 0, b: 2 }));
// => 'no match'在这个示例中,_.cond() 创建了一个复合函数 func,它根据输入对象的属性匹配情况返回不同的结果。根据对象的属性 'a' 和 'b',func 将返回相应的结果字符串。