Hi Dietrich,
On 10/30/2013 01:01 PM, Dietrich Streifert wrote:
> Hi all,
>
> I've been running into an error situation with an accidently forgotten
> comma behind the last element of an array initialization. Something like:
>
> var a = [
> { name: 'bla' },
> { name: 'blu' },
> ];
>
> console.log(a.length);
>
> If this code is executed in IE8 you get an array length of 3 whereas in
> IE9 and higher and all other current browsers the length seems to be 2.
> At least the error only shows up in IE8.
>
> I'm frequently using "generate.py on my project" to find code flaws like
> this, but unfortunately the linter in the toolchain did not detect the
> false code.
>
> Shouldn't lint warn about the dangling comma?
>
> Or is this a valid javascript code?
"Dangling commas" in arrays are not only permitted in the ES spec, they
have their own name, they're called "elisions". You can look at them as
if an 'undefined' value had been inserted at this position in the array.
So in that respect they are legal, and hence the lint job doesn't warn
(But we could add an option to the lint-check to enable this, if you
care to open a bug for it). The Generator also makes some effort to
retain elisions in optimized code, exactly because of the length semantics.
As for this array length issue, it really depends on which version of
the ES spec you comparing against. The JS 1.5 spec says about elisions:
"Array elements may be elided at the beginning, middle or end of the
element list. Whenever a comma in
the element list is not preceded by an AssignmentExpression (i.e., a
comma at the beginning or after
another comma), the missing array element contributes to the length of
the Array and increases the index
of subsequent elements. Elided array elements are not defined."
In that light IE6-8 (IIRC) conform very closely to that specification.
But the latest ES spec (ES, ed. 5.1) takes a slightly different approach:
"Array elements may be elided at the beginning, middle or end of the
element list. Whenever a comma in the
element list is not preceded by an AssignmentExpression (i.e., a comma
at the beginning or after another
comma), the missing array element contributes to the length of the Array
and increases the index of
subsequent elements. Elided array elements are not defined. If an
element is elided at the end of an array,
that element does not contribute to the length of the Array."
The two paragraphs are identical except there is an extra sentence at
the end of the newer version. In that version elisions *at the end* of
the array do not contribute to the array length. So in that light, more
current browsers are right and are conformant to the newer spec. :-)
HTH,
T.
------------------------------------------------------------------------------
Android is increasing in popularity, but the open development platform that
developers love is also attractive to malware creators. Download this white
paper to learn more about secure code signing practices that can help keep
Android apps secure.
http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
_______________________________________________
qooxdoo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel