Add an extra comma to break IE

| | Comments (0) | TrackBacks (0)

As a fan of Perl, I'm very used to lists of items that are written with an extra comma. For example:

my %hash = (
     foo => 1,
     bar => 2,
     baz => 3,
);

Even though there's not a fourth item in the list above, it still works. This is commonly referred to as the "trailing comma." Switching languages, the trailing comma is supported by Mozilla's JavaScript 1.5 implementation
. It is not, however, supported by IE (not even IE 7).

Therefore, when I perform a very similar operation as above, I have a nasty habit of forgetting this and breaking my code in IE:

var hash = {
    foo: 1,
    bar: 2,
    baz: 3,
};

The above runs fine in Firefox, but will fail to run with an error in IE 7. When supporting IE, you (I) must remember to remove the trailing comma:

var hash = {
    foo: 1,
    bar: 2,
    baz: 3
};

Okay, so some newb is probably reading this wondering, why is the trailing comma valid at all? Simple. It makes it just a little easier to add another element to the list later and even to reorder with copy and paste line-by-line. It can save just a small bit of time coding. That is, I could paste "qux: 4," into the first JavaScript example above foo, below foo, below bar, or below baz without worrying about it if I am able to use a trailing comma. In the second example, I can insert it above foo, below foo, below bar, but not below baz. Furthermore, after adding the comma after "baz: 3," I still cannot put it after baz until I remove the comma from the end of the pasted "qux: 4." This might save me only 20 seconds in my coding of paste, click, comma, click, backspace, but a little thing like this goes a long way in helping my sanity.

Anyway, I just thought I'd post this real quick in the hopes that I'll remember next time.

Cheers.

0 TrackBacks

Listed below are links to blogs that reference this entry: Add an extra comma to break IE.

TrackBack URL for this entry: http://contentment.org/mt/mt-tb.cgi/537

Leave a comment

About this Entry

This page contains a single entry by Andrew Sterling Hanenkamp published on November 17, 2006 11:52 AM.

Mac Misconceptions was the previous entry in this blog.

VMware's worst feature: Ctrl-R to reboot is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.