The Underscore.js _.arity() method returns a new function which is equivalent to the given function, except that the new function’s length property is equal to the number of Arguments. This does not limit the function to using that number of arguments. Its only effect is on the reported length.
Syntax:
_.arity( numberOfArgs, fun )
Parameters: This method takes two parameters as listed above and discussed below:
- numberOfArgs: This parameter takes a number stating the number of arguments the method will take.
- fun: This is the given function which is passed as parameter.
Return Value: It 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:
// Defining underscore contrib variable var _ = require( 'underscore-contrib' ); // Function function fun() { return "GFG" ; } var gfgFunc = _.arity(3, fun); console.log( "Length of function is :" , gfgFunc.length); console.log( "Function content :" , gfgFunc()); |
Output:
Length of function is : 3 Function content : GFG
Example 2:
// Defining underscore contrib variable var _ = require( 'underscore-contrib' ); // Function function fun() { return arguments[0]*10; } var gfgFunc = _.arity(3, fun); console.log( "Length of function is :" , gfgFunc.length); console.log( "Function content :" , gfgFunc(10)); |
Output:
Length of function is : 3 Function content : 100