Lodash - bind method
Syntax
_.bind(func, thisArg, [partials])
Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives.
Arguments
func (Function) − The function to bind.
thisArg (*) − The this binding of func.
[partials] (...*) − The arguments to be partially applied.
Output
(Function) − Returns the new bound function.
Example
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);
Save the above program in tester.js. Run the following command to execute this program.
Command
\>node tester.js
Output
BinderObject : Welcome
lodash_function.htm
Advertisements