“Tested only in FireFox” signature error

I've seen a particular mistake several times recently. It is a subtle ECMAScript spec violation that uncovers what browser engine was used during script development and testing: only Gecko-based browsers such as FireFox and presumably Mozilla allow it.

The error is to add a final comma inside an ECMAScript object literal:

var myObject = { property:'value' , }

Last seen in here and in an ad-hoc Chinese IME JavaScript demo (!) which I can't re-locate at the moment.

Try it out with the following bookmarklet:

javascript:try{eval("var a={a:'a',};");alert('Wrong behaviour!')}catch(e){alert('Correct behaviour!')}

A first-class example of how a minor browser sloppiness creates a serious compatibility problem.

Advertisements

5 thoughts on ““Tested only in FireFox” signature error

  1. Allowing trailing commas in object literals is a SpiderMonkey extension to ECMA-262 (yes, I asked about this), so parsing them and not throwing a SyntaxError is allowed per section 16 (first bullet of second list).

  2. I think you need to read the ECMAScript spec a little more closely… Mozilla follows the specs, IE breaks it. You can have as many commas as you want, whether or not there's an element preceding or following it. The relevant section of the specs is on page 40 of Ecma-262.pdfAccording to the spec only commas preceded by void elided elements add to the length of the array, not the last one that's followed by a void elided element. If you follow the Semantics with the examples [], [,] and [,,] you'll see that Mozilla is correct.

  3. cearrach: And you need to read the post you're replying to a bit more carefully. Hallvord is talking about object initialisers, not array initialisers. And ECMA-262 3 ed does not allow object initialisers to contain trailing commas or elisions, in difference to the array initialisers.I believe ES4 is going to allow this trailing comma. (At least that's the impression people are giving me on es4-discuss…)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s