Beware trailing comma in JavaScript prototypes

Onion Blog

Syndication

I spent more time than I care to admit tracking down this one, perhaps this post will save someone else the trouble...

When defining a number of functions in a prototype in JavaScript, do not include a trailing comma after the last function:
MyType.prototype = {
    foo : function() {
          // ...
    },

    bar : function() {
        //...
    }, //<- fails in IE!
 }
What was especially tricky about tracking this problem down was that FireFox works with or without the trailing comma, so it only fails in IE!

Posted Jun 19 2007, 06:52 AM by fritz-onion
Filed under:

Comments

Christopher Steen wrote Link Listing - June 19, 2007
on 06-19-2007 6:45 PM
Accessing and Updating Data in ASP.NET 2.0: Deleting Data [Via: ] Adobe AIR Bus Tour [Via: Dion Almaer...
Tobias wrote re: Beware trailing comma in JavaScript prototypes
on 06-25-2007 1:45 PM
In these cases, JSLint - http://www.jslint.com/ - is your friend.
Alex Le wrote re: Beware trailing comma in JavaScript prototypes
on 09-16-2007 5:39 AM
Good old "Page Request error" in IE and of course you cannot see the error because IE redirects to the "DNS error". IE7 is still a disaster for developers

Mitch wrote re: Beware trailing comma in JavaScript prototypes
on 05-22-2008 10:13 AM
You just cured my headache!

Thanks!
Rijk van Haaften wrote re: Beware trailing comma in JavaScript prototypes
on 07-23-2008 2:15 AM

Alternative notation:

<code>

var x =

 [ 1

 , 2

 , 3

 , 4

 ];

</code>

Applied to the code above:

<code>

MyType.prototype =

{ foo : function() {

         // ...

  }

, bar : function() {

       //...

  } //

}

</code>

dragffy.com » Blog Archive » Beware of the Trailing Comma in JavaScript Prototypes wrote dragffy.com &raquo; Blog Archive &raquo; Beware of the Trailing Comma in JavaScript Prototypes
on 12-03-2008 4:29 AM

Pingback from  dragffy.com  » Blog Archive   » Beware of the Trailing Comma in JavaScript Prototypes

Add a Comment

(required)  
(optional)
(required)  
Remember Me?