Lodash _.wrap() Method
Lodash _.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 the binding of the function formed.
Syntax:
_.wrap(value, [wrapper=identity]);
Parameters:
- value: It is the value to be wrapped.
- wrapper: It is the wrapper function.
Return Value:
This method returns the new function.
Example 1: In this example, we are using the lodash _.wrap() method passing the escape method and a custom function to it, and printing the result in the console.
Javascript
// Requiring lodash library const _ = require( 'lodash' ); // Calling wrap() method with its parameter let res = _.wrap(_.escape, function (functn, txt) { return '<b>' + functn(txt) + '</b>' ; }); // Assigning values console.log(res( 'GfG, Beginner, & w3wiki' )); |
Output:
<b>GfG, Beginner, & w3wiki</b>
Example 2: In this example, we are using the lodash _.wrap() method passing the upperCase method and a custom function to it, and printing the result in the console.
Javascript
// Requiring lodash library const _ = require( 'lodash' ); // Calling wrap() method with its parameter let newfn = _.wrap(_.upperCase, function (x, y) { return x(y) }); // Assigning values console.log(newfn( "w3wiki" )); |
Output:
w3wiki
Reference: https://lodash.com/docs/4.17.15#wrap
Contact Us