Lodash - constant 方法

语法

_.constant(value)

创建一个返回值的函数。

参数

  • value (*) − 新函数要返回的值。

输出

  • (Function) − 返回新的常量函数。

示例

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

console.log(check({ 'a': 1, 'b': 2 }));
console.log(check({ 'a': 0, 'b': 1 }));
console.log(check({ 'a': '1', 'b': '2' }));

将上述程序保存在tester.js中。运行以下命令执行该程序。

命令

\>node tester.js

输出

matches A
matches B
no match

lodash_util.html