Hey, get in line

Published on den 21 October 2010

Today when I was applying the module pattern in my javascript I got an error saying "invalid label" in Firebug.

Sometimes habits from different languages work lovely toghether but there are times when these habits causes you code to go haywire. My last run-in with on of these habits happend when I was trying to add a global javascript file to one of my sites. To make sure I wouldn't polute the global namespace (read why here or just google it) I was applying the module pattern. Now to my suprise it wasn't working. It said my namespace was not defined and because it wasn't my first time doing this I got pretty suprised. At the time I only had one method and I didn't have the energy to investigate it so I dropped the code directly into global namespace instead.

Today I had the need to add more methods so I came back to this file. After adding my methods and updating the return statement it looked like this:

return
{
    initMenu: initMenu,
    showValidationErrors : showValidationErrors,
    getValidationErrorDOM : getValidationErrorDOM
}

Now that looked just great to me but when I ran the site I got the following error in firebug:

*invalid label* at the row *showValidationErrors : showValidationErrors*

After a while I remebered that in Javascript you want to place your leading curly brace on the end of the line and not on the next line. So I changed my code to:

return{
    initMenu: initMenu,
    showValidationErrors : showValidationErrors,
    getValidationErrorDOM : getValidationErrorDOM
}

And everything worked just great again. The reason for this is that javascript automatically adds semicolons to some statements if there is none. And because return; certainly would be valid I guess that is what happend to me.

Then feel free to it or if you have any comments or questions mention @MikaelEliasson on Twitter.

CTO and co-founder at Bokio with a background as an elite athlete. Still doing a lot of sports but more for fun.

#development, #web, #orienteering, #running, #cycling, #boardgames, #personaldevelopment