Lodash - bind 方法
語法
_.bind(func, thisArg, [partials])
建立一個函式,該函式呼叫 func,其中 thisArg 繫結和 partials 作為收到的引數的字首。
引數
func (函式) − 要繫結的函式。
thisArg (*) − func 的 this 繫結。
[partials] (...*) − 要部分應用的引數。
輸出
(函式) − 返回新的繫結函式。
示例
var _ = require('lodash');
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
lodash_function.htm
廣告