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: