Underscore.JS - bind 方法
语法
_.bind(function, object, *arguments)
bind 方法有助于用传递的对象的引用替换函数中 this 的出现。参见以下示例
示例
var _ = require('underscore');
var updateMessage = function(message) {
return this.name + ' : ' + message;
}
//将 this 与提供的对象绑定
updateMessage = _.bind(updateMessage, {name: 'BinderObject'}, "Welcome");
var result = updateMessage();
console.log(result);
将上述程序保存在tester.js中。运行以下命令执行该程序。
命令
\>node tester.js
输出
BinderObject : Welcome
underscorejs_functions.html