Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to determine if function has arguments? [duplicate]

Tags:

javascript

Is it posible to know, if my function takes vars?

For example:

function ada (v) {};
function dad () {};
alert(ada.hasArguments()); // true
alert(dad.hasArguments()); // false
like image 544
ovnia Avatar asked Jan 14 '14 20:01

ovnia


People also ask

How do you check if a function has an argument?

To check if a parameter is provided to a function, use the strict inequality (! ==) operator to compare the parameter to undefined , e.g. if (param !== undefined) . If the comparison returns true , then the parameter was provided to the function.

Can a function take multiple arguments?

Functions can accept more than one argument. When calling a function, you're able to pass multiple arguments to the function; each argument gets stored in a separate parameter and used as a discrete variable within the function.

How many arguments can function have?

Our functions should have the minimum number of arguments possible, if it have less than four argument, nice. Put a limit on the argument's number is not the ideal, we should strive to keep them as minimal as we can.

How do you know if an argument is undefined?

So the correct way to test undefined variable or property is using the typeof operator, like this: if(typeof myVar === 'undefined') .


2 Answers

Yes. The length property of a function returns the number of declared arguments:

alert(ada.length); // 1
alert(dad.length); // 0
like image 178
p.s.w.g Avatar answered Sep 28 '22 14:09

p.s.w.g


The function's length property represents the number of formal parameters. Note that this is not necessarily equal to the number of actual parameters:

function foo(one, two, three) {
    return foo.length === arguments.length;
}

foo("test");
foo("test", "test", "test");

Output:

false
true
like image 37
Wayne Avatar answered Sep 28 '22 13:09

Wayne