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, & 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