-
Whatever. For one, foo is not an build-in function so the reference to this part of the specs is not substantial. The linked codeburst.io article states "arguments.length indicates the exact number of arguments which does not depend on the number of parameters in the function definition.".
Maybe your understanding of the spec is correct. But all other JavaScript implementations have a different understanding. Being the only to follow the spec is a bug - from the user point of view. Because it's incompatible with the rest of the world.
Sat 2019.08.17
What basis is being used to make that determination?
The behavior of
arguments
is exactly as designed. Was the Note beneath the arguments object actually read?Anybody check the spec?
See section 17 ECMA spec
Also check out sections 14, 6 and 9 on the argument and exotic objects along with function definitions
Specifically this passage:
"if a built-in function or constructor is given fewer arguments than the function is specified to require, the function or constructor shall behave exactly as if it had been given sufficient additional arguments, each such argument being the undefined value."
There seems to be a mis-understanding of the parameter list and what an argument is. Here is an easy read:
We the coder define the parameter list that a function container will process. In this manner, the function may act on pre-determined arguments (the value of a parameter) knowing the exact parameter count.
By design, and specifically addressed in the ECMA specification, when fewer arguments (the values) are passed to a function through it's parameter list, those arguments take on a value of undefined.
While there may be rare instances when more arguments are passed, it can be seen that without the defined parameter list, the function won't have sufficient information on what to do with the additional un-linked values.
Back to the original function from #1 as defined with two parameters:
Here we have exactly two parameters defined in function
foo(x,y)
. When we pass two arguments, then the indexer of the arguments array container will correctly report what is passed.When more arguments are passed, we can see that the count matches as it should.
But, when using fewer arguments, per the specification, the omitted parameter arguments contain the value undefined. As the function requires valid values and as spelled out in the note from #2 link above, the arguments array will report what is defined in the function parameter list.
The reason the error
appears is from incorrect Javascript as I pointed out in detail within the #2 explanation. Two obvious flaws here. Attempting to pass a different number (1) of arguments to a function that is defined (2) by the coder, then expecting an array (1 valid) to produce valid output, when the rules to passing (only 1) the correct number of parameters contradicts the exact definition (2) the coder intended.
That non-array element request is similar to this snippet
As passing one argument to a two parameter list will result in an undefined value, the arguments array is unable to produce a valid response for the second value as there is only one valid argument that the call to function foo() passes. Therefore the array indexer only contains one reference and that is to element[ 0 ]. Attempting to print out to the console an un-referenced element[ 1 ] therefore will produce the error that is introduced from a bad coding practice.