The Lodash _.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.
Return Value: It returns a new function.
Note: To execute the below examples, you have to install the lodash-contrib library by using this command prompt and execute the following command.
npm install lodash-contrib
Example 1:
Javascript
// Defining lodash contrib variable var _ = require( 'lodash-contrib' ); // Function function fun(){ return "neveropen" ; } var gfgFunc = _.arity(4,fun); console.log( "Length of function is :" , gfgFunc.length); console.log( "Function content :" , gfgFunc()); |
Output:
Length of function is : 4 Function content : neveropen
Example 2:
Javascript
// Defining lodash contrib variable var _ = require( 'lodash-contrib' ); // Function function fun(){ return arguments[0]*10; } var gfgFunc = _.arity(4,fun); console.log( "Length of function is :" , gfgFunc.length); console.log( "Function content :" , gfgFunc(10)); |
Output:
Length of function is : 4 Function content : 100