Repeat after me: Javascript is not Python

I guess I’m too used to the Python programming language that whenever I have to write simple Javascript functions I keep making the same mistakes. I tend to write as much functionality as possible in the server side (Python :) ) but in order to create rich user interfaces I have to use the client side (Javascript :( ) and things like this happens:

function addTopic(topic) {
    var input_attrs = {
        type: "checkbox",
        checked: "checked",
        name: "topics",
        value: topic,
    };
    var li = Builder.node('li', [Builder.node('label', [Builder.node('input', input_attrs), topic.title])]);
    $("topic_list").appendChild(li);
}

For those of you who are curious, yes I’m using Script.aculo.us and Prototype libraries here but here is the point: There is a syntax error in the previous code, one that Firefox (and thus Firebug) won’t catch but one that Internet Explorer will. The funny thing is the Internet Explorer error message:

Error: Expected identifier, string or number

So, anybody can answer me: is the trailing comma a real Javascript syntax error or just another Internet Explorer flaw?

3 Responses to “Repeat after me: Javascript is not Python”

  1. enlavin Says:

    IE does not support trailing commas in the definition of a dictionary. FF and Opera does. It’s very funny to debug until you know that :D

    Try http://www.jslint.com/ to find those subtle errors.

  2. lgs Says:

    Thanks for the info, enlavin! And also for the great link.

  3. Sean Upton Says:

    Ah, trailing commas… Been there, done that. When I first saw this problem in my own code, I thought MSIE must have diverged from the ECMAscript spec, but no, it really wasn’t allowed in the spec. Funny how us python folks get tripped up on this…

Leave a Reply