Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.mixin() Function

Underscore.js _.mixin() Function

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 = "neveropen for neveropen";
        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 = "neveropen for neveropen";
        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:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments