cond source npm

_.cond(pairs)

创建一个函数。 这个函数会遍历 pairs,并执行最先返回真值对应的函数,并绑定 this 及传入创建函数的参数。

参数

  1. pairs (Array)

    判断函数对

返回值 (Function)

返回新的函数

示例

var func = _.cond([
  [_.matches({ 'a': 1 }),           _.constant('matches A')],
  [_.conforms({ 'b': _.isNumber }), _.constant('matches B')],
  [_.constant(true),                _.constant('no match')]
]);

func({ 'a': 1, 'b': 2 });
// => 输出:'matches A'

func({ 'a': 0, 'b': 1 });
// => 输出:'matches B'

func({ 'a': '1', 'b': '2' });
// => 输出:'no match'