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
广告