The _.flip2() method returns a function that works identically to given function but accepts the first two arguments in reverse order. The order of all rest arguments remains the same.
Syntax:
_.flip2( 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 = _.flip2(gfgFunc); Â Â console.log(abc(1, 2, 3)); |
Output:
Value of a is 2 and Value of b is 1 and Value of c is 3
Example 2:Â
Javascript
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );Â Â Â function gfgFunc(a, b) { Â Â Â Â return b + " is " + a; } Â Â var gfg = _.flip2(gfgFunc); Â Â console.log(gfg( "neveropen" ,+ Â Â Â "Computer Science Portal for Geeks" )); |
Output:
neveropen is Computer Science Portal for Geeks