Underscore.js _.unary() Method
The Underscore.js _.unary() method returns a new function that accepts only one argument and passes this argument to the given function. Additional arguments are discarded.
Syntax:
_.unary( fun )
Parameters: This method takes a single parameter as listed above and discussed below:
- fun: This is the given function passed as parameter.
Return Value: This method returns a new function.
Note: To execute the below examples, you have to install the underscore-contrib library by using this command prompt and execute the following command.
npm install underscore-contrib
Example 1:
var _ = require( 'underscore-contrib' );
function fun() {
return arguments;
}
var gfgFunc = _.unary(fun);
console.log( "Arguments are :" ,
gfgFunc(1, 2, 3));
|
Output:
Arguments are : [Arguments] { '0': 1 }
Example 2:
var _ = require( 'underscore-contrib' );
function fun() {
return arguments;
}
var gfgFunc = _.unary(fun);
console.log( "Arguments are :" ,
gfgFunc(1, 2, 3, 4, 5, 6, 7, 8));
|
Output:
Arguments are : [Arguments] { '0': 1 }
Example 3:
var _ = require( 'underscore-contrib' );
function fun(a){
return a;
}
var gfgFunc = _.unary(fun);
console.log( "Best Platform :" ,
gfgFunc( "GeeksforGeeks" ));
|
Output:
Best Platform : GeeksforGeeks
Last Updated :
18 Sep, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...