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.

Leave a comment