Open In App

Underscore.js _.mixin() Function

Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy. The _.mixin() function is used to add extra functionality and extend the global underscore object to some special utility functions.

Its important to link the underscore CDN before going and using underscore functions in the browser. When linking the underscore.js CDN The “_” is attached to the browser as a global variable.

Syntax:

_.mixin( object )

Parameters: This function accepts single parameter i.e the object.

Return Value:

Example 1:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script>
  
        // Function to be binded with
        // the global "_" object
        function stringtoArray(str) {
  
            // Split the string to array
            return str.split("");
        }
        _.mixin({
  
            // Sta is a variable acronym
            // for string to array
            sta: stringtoArray
        })
  
        let str = "geeks for geeks";
        let arr = _.sta(str);
  
        console.log(`string is: ${str}`)
        console.log(
            `array formed from string is:`, arr);
    </script>
</body>
  
</html>


Output :

Example 2: If no parameter passed to the random function.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script>
        _.mixin({
  
            // Substring function that takes string
            // starting index and end index
            substring: (str, s, l) => {
                return str.split("").splice(s, l).join("");
            }
        })
        let str = "geeks for geeks";
        let substr = _.substring(str, 9, 6);
  
        // Print original string
        console.log(`string is: ${str}`)
  
        // Print substring
        console.log(
            `substring formed from string is:`, substr);
    </script>
</body>
  
</html>


Output:



Last Updated : 09 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads