Underscore.JS - 绑定方法



语法

_.bind(function, object, *arguments)

bind 方法有助于将函数中 this 的出现替换为传递的对象引用。请参见以下示例

示例

var _ = require('underscore');

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

//Bind this with object provided
updateMessage = _.bind(updateMessage, {name: 'BinderObject'}, "Welcome");
var result = updateMessage();
console.log(result);

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

命令

\>node tester.js

输出

BinderObject : Welcome
underscorejs_functions.htm
广告
© . All rights reserved.