Wednesday, July 3, 2024
HomeLanguagesJavascriptLodash _.wrap() Method

Lodash _.wrap() Method

The _.wrap() method of Function in lodash is used to create a function that delivers value to the stated wrapper like its initial argument. Moreover, any further arguments delivered to the function are added to those which are delivered to the stated wrapper.

Note:

  • The wrapper used here is called with the this binding of the function formed.

Syntax:

_.wrap(value, [wrapper=identity])

Parameters: This method accepts two parameters as mentioned above and described below:

  • value: It is the value to be wrapped.
  • wrapper: It is the wrapper function.

Return Value: This method returns the new function.

Below examples illustrate the Lodash _.wrap() method in JavaScript:

Example 1:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Calling wrap() method with its parameter
var res = _.wrap(_.escape, function(functn, txt) {
  return '<b>' + functn(txt) + '</b>';
});
   
// Assigning values
res('GfG, neveropen, & neveropen');


Output:

<b>GfG, neveropen, &amp; neveropen</b>

Example 2:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Calling wrap() method with its parameter
var newfn = _.wrap(_.upperCase, function(x, y) {
  return x(y)
});
  
// Assigning values
newfn("neveropen");


Output:

GEEKSFORGEEKS

Reference: https://lodash.com/docs/4.17.15#wrap

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!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments