Lodash - bind 方法

语法

_.bind(func, thisArg, [partials])

创建一个函数,该函数使用 thisArg 的 this 绑定和在其接收的参数前面添加的 partials 来调用 func。

参数

  • func (Function) − 要绑定的函数。

  • thisArg (*) − func 的 this 绑定。

  • [partials] (...*) −要部分应用的参数。

输出

  • (Function) − 返回新绑定的函数。

示例

var _ = require('lodash');
var updateMessage = function(message) {
    return this.name + ' : ' + message;
}

//将此与提供的对象绑定
updateMessage = _.bind(updateMessage, {name: 'BinderObject'}, "Welcome");
var result = updateMessage();
console.log(result);

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

命令

\>node tester.js

输出

BinderObject : Welcome

lodash_function.html