1

I know arguments.length method , but this returns the number of arguments the function was called with.

What I need instead is the number of arguments that function requires, so for example

function myFunc(arg1,arg2,arg3) {
  alert("number of passed args:" + arguments.length);
  alert("number of required args:" + /*the code I'm asking for*/);
};

myFunc('some arg');

So myFunc() would return two alerts saying 1 and 3.

Any way to do that ?

rsk82
  • 28,217
  • 50
  • 150
  • 240

1 Answers1

3

Another, he deprecated arguments.caller.length property holds this value.

function myFunc(arg1,arg2,arg3) {
    alert("number of passed args:" + arguments.length);
    alert("number of required args:" + arguments.caller.length);
}

Another method to get the number of formal parameters is by using the length property of the function. The function's name has to be known for this method:

function myFunc(arg1,arg2,arg3) {
    alert("number of passed args:" + arguments.length);
    alert("number of required args:" + myFunc.length);
}
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • About this 'deprecated' is not so simple: http://stackoverflow.com/a/1335595/393087 , they changed name for it , functionality will be saved. (as I understand it thru my leaky english skills) – rsk82 Dec 29 '11 at 21:49
  • @rsk82 `arguments.callee.caller` points to the calling function, not the function itself. – Rob W Dec 29 '11 at 21:53