sab669 sab669 - 3 months ago 11
Javascript Question

Will this statement ever evaluate to true?

I'm new-ish to Javascript and need to make a change to this one script. I noticed this line of code that thoroughly confused me, however.

function FIValidateForm(aForWhom, aDoNotShowWarning, aFromWhere)
{
try
{
PAIWarnings = '';
var pBlFalg = true;

if (pBlFalg)
pBlFalg = mfpCheckForFISave(aForWhom, aDoNotShowWarning, aFromWhere);

if (pBlFalg == true)
pBlFalg = PAIErrors();

if ((pBlFalg) && (FIValidateForm.arguments.length == 1))
mfpIFWarnings();

return pBlFalg;
}
catch (err)
{
logError(err, arguments.callee.trace());
}
}


During run time, if I put a breakpoint on that third
if
statement and inspect
FIValidateForm.arguments
I can see an array with 3 items. The first contains a
string
, the second is
null
, and the third is
undefined
. The array still has a length of 3.

Am I correct in assuming that no matter what one were to pass to this method,
FIValidateForm.arguments.length == 1
will always be
false
? Or is there some other value I'm unaware of / alternative way to call this method so that
arguments.length
would equal 1?

Edit: I see JS has an
arguments.length
and a
Function.length
. The latter returns the expected number of parameters... So then how does one call a method so that the value would be 1 when the function is defined with 3?

Answer

Am I correct in assuming that no matter what one were to pass to this method, FIValidateForm.arguments.length == 1 will always be false?

No, you can make its FIValidateForm.arguments.length 1 by doing this:

FIValidateForm(justOneArgHere);

arguments represents the actual arguments to the function. (See 1 below about arguments vs. FIValidateForm.arguments.)

If you want the function's arity (the number of formal declared arguments), that's the FIValidateForm.length and it doesn't vary.

So then how does one call a method so that the value would be 1 when the function is defined with 3?

JavaScript doesn't enforce the number of formal arguments at all; you can call your function with no arguments, one argument, or 15. If a formal argument (a declared one, like your aDoNotShowWarning) is not supplied, the value it will have within the function call will be undefined. If you supply more arguments than are declared, they are only accessible (in ES5 and below) via the arguments pseudo-array. (In ES2015 and above, you can make your last argument a "rest argument", which means it will be an array of all arguments from that point on.)


Example:

function foo(a, b, c) {
  console.log(arguments.length);
  console.log(foo.length);
}
foo(1);
foo(1, 2);


1 The arguments property of the function (e.g., FIValidateForm.arguments) is a non-standard extension to JavaScript, which is expressly deprecated by strict mode (the example above would throw an error in strict mode). Just use the freestanding symbol arguments, which is defined for normal functions (as though it were a local variable).

(And in ES2015 or higher, you can avoid using arguments at all by using rest notation on your argument declarations instead.)

Comments