Lodash - bindKey 方法



語法

_.bindKey(object, key, [partials])

建立使用 object[key] 中的方法的函式,並將部分引數前置到其所接收的引數之前。

引數

  • object(物件) - 要在其上呼叫方法的物件。

  • key(字串) - 方法的鍵。

  • [partials](...*) - 部分應用的引數。

輸出

  • (函式) - 返回新的繫結函式。

示例

var _ = require('lodash');
var object = {
   user: 'Joe',
   greet: function(message) {
      return this.user + ' : ' + message;
   }
};

//Bind this with object provided
updateMessage = _.bindKey(object, 'greet', "Welcome");
var result = updateMessage();
console.log(result);

tester.js 中儲存上述程式。執行以下命令以執行此程式。

命令

\>node tester.js

輸出

Joe : Welcome
lodash_function.htm
廣告
© . All rights reserved.