Open In App

Underscore.js _.wrap() Function

Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js _.wrap() is used to wrap a function inside another function. It means that the first calling function will be invoked first and then the called function will be executed. If the calling function does not call the called function then the second function will not be executed.

Syntax:

_.wrap(function, wrapper );

Parameters:

  • function: This parameter is used to hold the name of the function that has to be called.
  • wrapper: This parameter is used to hold the wrapper function which wraps the first function.

Return Value:

It returns the output of both the functions involved in the process.

Passing no arguments of both functions of _.wrap() function:

The function is called which is passed to the console.log() function. That function body contains the _.wrap() function which calls the other function. Then the other function body gets executed and finally the control returns to the called function.

Example: The below code example implements the _.wrap() function without passing arguments of both functions.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const func1 =
        function () { return "first function. "; };
 
        func2 = _.wrap(func1, function (func) {
            return func() + "second func. ";
        });
 
        console.log(func2());
    </script>
</body>
 
</html>


Output:

Passing an argument to the second function of _.wrap() function:

Passing the argument to the second function from the first function definition. In the definition, the parameter to the second function will be passed by giving the parameter inside double quotes (” “) inside the brackets. The function will work as earlier like first, the first function which is called will be executed and then the first function will be executed.

Example: The below code uses the _.wrap() function by passing the argument to the second function.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const func1 = function (a) {
            return "first function: " + a + ".";
        };
 
        func2 = _.wrap(func1, function (func) {
            return "second func starts. "
                + func("10") + " second function ends.";
        });
 
        console.log(func2());
    </script>
</body>
 
</html>


Output:

Passing special characters as argument to the second function of _.wrap() function:

Passing special characters to the second function then the _.wrap() function will work in the same way. It takes the special character as a normal character and then works in the same way. The first function will be called and then it’s body will be executed. After that when the first function is called inside the second function, the second function body will get executed.

Example: The below code use the characters as the argument to the second function of the _.wrap() function.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const func1 = function (a) { return a; };
 
        func2 = _.wrap(func1, function (func) {
            return "second function starts { "
                + func("***** This is first function *****")
                + " } second function ends here.";
        });
 
        console.log(func2());
    </script>
</body>
 
</html>


Output:

Passing numbers as arguments to the second function of the _.wrap() function:

The second function will also consider the numbers in the same way as they consider the characters. This will give the output accordingly.

Example: The below example passes a number as argument to the second function of the _.wrap() function.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const func1 = function (a) { return a + ", "; };
 
        func2 = _.wrap(func1, function (func) {
            return "10, 20, " + func("30, 40, 50") + " 60, 70.";
        });
 
        console.log(func2());
    </script>
</body>
 
</html>


Output:



Last Updated : 16 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads