Syntax, Parameter and Return Value
Syntax:
_.wrap(value, wrapper)
Parameters:
-
value (Function): The function to wrap.
- wrapper (Function): The wrapper function.
Return Value:
(Function) - Returns the new wrapped function.
Examples
Adding Logging to a Function:
JavaScript
var _ = require('lodash');
function multiply(a, b) {
return a * b;
}
var loggedMultiply = _.wrap(multiply, function(func, ...args) {
console.log('Multiplying:', args);
return func(...args);
});
console.log(loggedMultiply(5, 3)); // Logs 'Multiplying: [5, 3]' and outputs 15

You can also try this code with Online Javascript Compiler
Run Code
Outputs
15
Demonstrates wrapping a function to add logging capabilities.
Modifying Return Value:
JavaScript
function greet(name) {
return `Hello, ${name}`;
}
var excitedGreet = _.wrap(greet, function(func, ...args) {
return func(...args) + '!!!';
});
console.log(excitedGreet('Alice'));

You can also try this code with Online Javascript Compiler
Run Code
Outputs
'Hello, Alice!!!'
Shows how to wrap a function to modify its return value.
Error Handling in Function Execution:
JavaScript
function riskyOperation() {
// operation that might throw an error
}
var safeOperation = _.wrap(riskyOperation, function(func) {
try {
return func();
} catch (e) {
console.error('An error occurred:', e);
}
});
safeOperation();

You can also try this code with Online Javascript Compiler
Run Code
// Executes the operation safely with error handling
An example of wrapping a function for added error handling.
Performance Monitoring:
function processData(data) {
// process the data
}
var monitoredProcess = _.wrap(processData, function(func, ...args) {
const start = performance.now();
const result = func(...args);
const end = performance.now();
console.log(`Processing took ${end - start}ms`);
return result;
});
monitoredProcess({ data: 'sample' });

You can also try this code with Online Javascript Compiler
Run Code
// Executes and logs the processing time
Demonstrates wrapping a function to monitor its performance.
Frequently Asked Questions
Can _.wrap() be used with methods of objects?
Yes, _.wrap() can be used with object methods, allowing you to extend or modify the behavior of methods without changing the original object.
Is the original function still accessible after wrapping?
The original function is embedded within the wrapped function and is not directly accessible or modifiable from outside the wrapper.
How does _.wrap() compare to subclassing or inheritance?
_.wrap() is a functional programming approach that offers a more flexible and less intrusive alternative to subclassing or inheritance for extending functionality.
Conclusion
Lodash's _.wrap() method is a versatile tool for enhancing or altering the functionality of existing functions. It allows for the execution of additional code before or after the original function, enabling a wide range of applications from error handling to performance monitoring, without altering the original function's core logic.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.