The _.compose() function is used to return the composition of the list of functions. The list of the function returns the value of the function that follows.
Syntax:
_.compose(*functions)
Parameters: This function accept a single parameter as mentioned above and described below:
- functions: It contains the list of function that to be executed.
Return Value: It returns the composition of list of functions.
Below examples illustrate the _.compose() function in Underscore.js:
Example 1:
<!DOCTYPE html> < html > Â Â < head > Â Â Â Â < script type = "text/javascript" src = Â Â Â Â </ script > </ head > Â Â < body > Â Â Â Â < script type = "text/javascript" > Â Â Â Â Â Â Â Â var fun1 = function (addString) { Â Â Â Â Â Â Â Â Â Â Â Â return "Welcome to " + addString; Â Â Â Â Â Â Â Â }; Â Â Â Â Â Â Â Â Â Â var fun2 = function (GFG) { Â Â Â Â Â Â Â Â Â Â Â Â return GFG; Â Â Â Â Â Â Â Â }; Â Â Â Â Â Â Â Â var str = _.compose(fun1, fun2); Â Â Â Â Â Â Â Â Â Â console.log(str('neveropen')); Â Â Â Â </ script > </ body > Â Â </ html > |
Output:
Example 2:
<!DOCTYPE html> < html > Â Â < head > Â Â Â Â < script type = "text/javascript" src = Â Â Â Â </ script > </ head > Â Â < body > Â Â Â Â < script type = "text/javascript" > Â Â Â Â Â Â Â Â var fun1 = function (str1) { Â Â Â Â Â Â Â Â Â Â Â Â return 'Welcome' + str1 + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â '\nA computer science portal'; Â Â Â Â Â Â Â Â }; Â Â Â Â Â Â Â Â Â Â var fun2 = function (str2) { Â Â Â Â Â Â Â Â Â Â Â Â return ' to ' + str2.toLowerCase(); Â Â Â Â Â Â Â Â }; Â Â Â Â Â Â Â Â var str = _.compose(fun1, fun2); Â Â Â Â Â Â Â Â Â Â console.log(str('GEEKSFORGEEKS')); Â Â Â Â </ script > </ body > Â Â </ html > |
Output: