Lodash _.bindKey() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.bindKey() method of Function in lodash is used to create a function which 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 does 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: This method accepts three parameters as mentioned above and described below:



  • object: It is the object which is used to call the method on.
  • key: It is the key to be used in the method.
  • partials: It is the arguments which is to be partially applied. It is an optional parameter.

Return Value: This method returns the new bound function.

Example 1:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring lodash library
const _ = require('lodash');
  
// Defining object parameter of this method
var obj = {
  'author': 'Nidhi',
  'welcome': function(greet, mark) {
    return greet + ' ' + this.author + mark;
  }
};
  
// Using the _.bindKey() method 
// with its parameters
var bound_fun =
  _.bindKey(obj, 'welcome', 'Hello');
  
// Calling bound_fun by passing its value
bound_fun('!!');

chevron_right


Output:

Hello Nidhi!!

Example 2: Using a bound with the placeholder.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring lodash library
const _ = require('lodash');
  
// Defining object parameter of this method
var obj = {
  'portal': function(portal, mark) {
    return 'Welcome to ' + portal + mark;
  }
};
  
// Using the _.bindKey() method with its
// parameters and a placeholder
var bound_fun =
  _.bindKey(obj, 'portal', _, '!');
  
// Calling bound_fun by passing its value
bound_fun('GeeksforGeeks');

chevron_right


Output:

Welcome to GeeksforGeeks!

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.