Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptLodash _.bindKey() Method

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




// 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('!!');


Output:

Hello Nidhi!!

Example 2: Using a bound with the placeholder.

Javascript




// 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('neveropen');


Output:

Welcome to neveropen!

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments