The _.flip() method returns a function that works identically to given function but accepts the arguments in reverse order.
Syntax:
_.flip( function );
Parameters: This method takes a function that takes some arguments.
Return Value: This method returns a function.
Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.
underscore.js contrib library can be installed using npm install underscore-contrib –save.
Example 1:
Javascript
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );
function gfgFunc(a, b, c) {
return "Value of a is " +
a + " and Value of b is " + b + " and Value of c is " + c;
} var abc = _.flip(gfgFunc);
console.log(abc(1, 2, 3)); |
Output:
Value of a is 3 and Value of b is 2 and Value of c is 1
Example 2:
Javascript
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );
function gfgFunc(a, b) {
return b + " is " + a;
} var gfg = _.flip(gfgFunc);
console.log(gfg( "GeeksforGeeks" ,+
"Computer Science Portal for Geeks" ));
|
Output:
GeeksforGeeks is Computer Science Portal for Geeks