Lodash - constant 方法



語法

_.constant(value)

建立返回 value 的函式。

引數

  • value (*) - 要從新函式返回的值。

輸出

  • (函式) - 返回新的常量函式。

示例

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.htm
廣告
© . All rights reserved.