• Sat 2019.08.17

    'Yes it's a bug'

    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?

    https://www.espruino.com/Reference#l__gl­obal_arguments

    Anybody check the spec?

    http://www.ecma-international.org/public­ations/standards/Ecma-262.htm



    See section 17 ECMA spec

    http://www.ecma-international.org/ecma-2­62/10.0/index.html#sec-method-definition­s

    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:

    https://codeburst.io/parameters-argument­s-in-javascript-eb1d8bd0ef04



    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:

    function foo(x,y) {
      for ( var i=0; i < arguments.length; i++ ) {
        console.log( arguments[i] );
      }
    }
    

    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.

    foo(100);
    
    // is not passing two arguments as the function definition requires
    
    foo( x, y );
    
    // in this case 
    //  x is assigned the value 100 
    //  y is undefined
    
    // the arguments object only contains one valid argument
    

    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

    Uncaught ReferenceError: "1" is not defined
     at line 2 col 24
    console.log( arguments[i] );
    

    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

    var ary = new Array( 7, 8, 9 );
    
    console.log( ary.length );
    
    // attempt to print array values that were never defined
    for( var i=0; i<5; i++ ) {
      console.log( ary[ i ] );
    }
    
    
    The output as expected
    3
    
    
    7
    8
    9
    undefined
    undefined
    


    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.

  • 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.

  • 'Whatever. For one, foo is not an build-in function so the reference to this part'

    Yes, exactly. So why would a coder define a function with a specific number of parameters expecting an exact return response, then expect Javascript or any language that supports methods and functions, to magically to produce the same desired result when that same coder is only passing one valid argument?

About

Avatar for maze1980 @maze1980 started