Lodash _.bindKey() Method
Lodash _.bindKey() method of Function in lodash is used to create a function that calls the method at the object[key] along with the partials added to the arguments it accepts.
Note:
- This method is different from the _.bind() method as it permits bound functions to mention methods that may be reinterpreted or do not still exist.
- The _.bindKey.placeholder value that is by default ( _ ) in monolithic builds which is utilized as a placeholder for partially used arguments.
Syntax:
_.bindKey(object, key, partials);
Parameters:
- object: It is the object that is used to call the method.
- key: It is the key to be used in the method.
- partials: It is the arguments that are to be partially applied. It is an optional parameter.
Return Value:
This method returns the new bound function.
Example 1: In this example, we are passing partials to the function and printing the result into the console by the use of the lodash _.bindKey() method.
Javascript
// Requiring lodash library const _ = require( 'lodash' ); // Defining object parameter of this method let obj = { 'author' : 'Nidhi' , 'welcome' : function (greet, mark) { return greet + ' ' + this .author + mark; } }; // Using the _.bindKey() method // with its parameters let bound_fun = _.bindKey(obj, 'welcome' , 'Hello' ); // Calling bound_fun by passing its value console.log(bound_fun( '!!' )); |
Output:
Hello Nidhi!!
Example 2: In this example, we are passing partials to the function and using a bound with the placeholder then printing the result into the console by the use of the lodash _.bindKey() method.
Javascript
// Requiring lodash library const _ = require( 'lodash' ); // Defining object parameter of this method let obj = { 'portal' : function (portal, mark) { return 'Welcome to ' + portal + mark; } }; // Using the _.bindKey() method with its // parameters and a placeholder let bound_fun = _.bindKey(obj, 'portal' , _, '!' ); // Calling bound_fun by passing its value console.log(bound_fun( 'w3wiki' )); |
Output:
Welcome to w3wiki!