Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get number of arguments in function expression? [duplicate]

Tags:

javascript

Possible Duplicate:
Get a function’s arity

How can I declare a function expression, pass it into a defined function, and have the defined function determine how many arguments the function expression has?

See this code snippet for reference:

function getArgumentCount(fexp)
{
    return ...;
}

var fexp1 = function(a) { };
var fexp2 = function(a, b) { };

console.log(getArgumentCount(fexp1)); // Should output 1
console.log(getArgumentCount(fexp2)); // Should output 2
like image 355
Phil K Avatar asked Dec 02 '12 21:12

Phil K


People also ask

How can you get total number of arguments passed to a function?

length property contains the number of arguments passed to the function.

How do you find the number of arguments in Python?

We will use len() function or method in *args in order to count the number of arguments of the function in python.

How do you specify a parameter in a function when you do not know the number of values?

Arbitrary Arguments, *args If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition.

Can a function have variable number of parameters?

To call a function with a variable number of arguments, simply specify any number of arguments in the function call. An example is the printf function from the C run-time library. The function call must include one argument for each type name declared in the parameter list or the list of argument types.


1 Answers

javascript functions have a .length property

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/length

like image 185
Alex Avatar answered Sep 17 '22 07:09

Alex