Skip navigation.

exploreopera

| Help

Sign up | Help

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

Microsoft's vista of tag soupintervals and timeouts quirks

Comments

avatar
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).

By Waldo, # 4. October 2005, 06:32:46

avatar
allowing syntax errors is a feature now ?!? how !!
hurray SpiderApe!

By xErath, # 6. January 2006, 04:47:25

avatar
Yes, implementations are actually allowed to extend the syntax but it harms interoperability on the web.

By hallvors, # 6. January 2006, 14:46:01

avatar
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.pdf

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

By cearrach, # 21. April 2008, 18:41:40

avatar
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...)

By liorean, # 25. April 2008, 12:57:19

Write a comment

You must be logged in to write a comment. if you're not a registered member, please sign up.