• Mon 2019.08.19

    Thank you @allObjects for the link to the thirty or so Javascript engines. An eye openner. The Douglas Crockford book brings back early memories.

    reply to @maze1980 #12 'So there are two bugs' and 'arguments fails for undefined arguments (not documented)'

    which of course is not true

    Refer to link in #5 'ECMA specification'

    Beneath heading 'The Reference Specification Type'

    "A base value component of undefined indicates that the Reference could not be resolved to a binding"

    Beneath heading 'Well-Known Intrinsic Objects'

    "If Type(V) is not Reference, throw a ReferenceError exception"

    Beneath heading 'ReferenceError'

    "Indicate that an invalid reference value has been detected"

    Using code snippet from #12 L1 clearly defines a function requiring two valid initialized primitive types. It is the designers responsibility to validate that the expected data, matches the function definition. It is also the responsibility of the caller to pass valid data matching the function prototype. Specification recommendations.

    L7 violates passing initialized arguments to a known function that defines that requirement. Although a bad practice, it is allowable and valid Javascript. It is an 'unresolved binding issue', however.

    The trap is now set.

    No where in lines L2 through L6 is there a validation check on the arguments passed to the function. Javascript allows this as the syntax is valid, although also a bad practice. As four primitive types are passed at L7, they initialize the arguments object, with elements [0] and [2] set to the 'undefined' primitive type. Another bad practice.

    Also verified by the Mozilla group MDN web docs.

    "failure to declare variables will very likely lead to unexpected results. Thus it is recommended to always declare variables, regardless of whether they are in a function or global scope"

    Note that even with element indicies, object arguments is it's own object type and not derived from object Array

    Review the last snippet in #5 demonstrating undefined array elements for the following:

    L4 springs the trap.   

    A request within valid Javascript statement console.log() to access an undefined element, the first in the loop being element [0]. The statement attempts to extract a Reference to a base value that is never initialized. A rule violation. This event is detected within Javascript producing a 'detected invalid reference'. In response to and per specification, Javascript creates the 'ReferenceError' object, populates it and throws the exception which is clearly seen in L10 and L14 revealing the suspect elements.

    The output is as expected and exactly to ECMA specification. No bugs.

    Thank you @maze1980 for presenting your snippet in #12 that demonstrates careless coding deficiencies that can lead to undesired results.

    One other possible solution is to implement the redefinition example @allObjects provided in last pp. in #13, . . . and;

    Again my recommendation is to use the argument validation code as suggested in the fully functional working example within #11 to avoid attempting to access objects that are not properly initialized.


Avatar for Robin @Robin started