Twitter GitHub Facebook Instagram dirv.me

Daniel Irvine on building software

6 things you wish you knew about JavaScript before you started. #5 will blow your mind.

18 August 2014

1. Object literal syntax, which gave birth to JSON

JSON is the one of the world’s most popular data interchange formats. It has stood the test of time against the behemoth that is XML, mainly due to its minimalist approach. It showed us how great the world can be without “design by committee”. So we’re very grateful to JavaScript for giving us this.

Happy hour is from 4 to 6

2. JavaScript is class-free

Prototypal inheritance is conceptually simpler than classical OO and can lead to neat solutions. Still not widely understood, some people believe prototypal inheritance will have its day in the not-so distant future. Where'd all the files go?

3. this late binding

Methods in JavaScript are reusable in a totally new way because this is bound at invocation time to the calling. Many developers work around this by using techniques such as var this = that, but actually this late binding technique creates a whole new world of possibility. It ties in nicely with prototypal inheritance: unbound methods exist as a cross between abstract classes and interfaces. Unfortunately, anyone who publicly declares it as awesome is likely to be judged insane.

In my world, everyone's a pony

4. JavaScript does not have block scope

JavaScript uses the characters { and } to denote a block, just like C. But unlike C, JavaScript doesn’t have block scope, one of the primary reasons for using blocks in the first place. D’oh. This is a serious source of confusion for experienced programmers coming to the language for the first time.

PC Load Letter?

5. Forgetting to use the new keyword for constructor calls clobbers global variables

In sane languages, forget to include the new keyword in a constructor call and you’ll get a syntax error. But in JavaScript, calling a constructor without new is perfectly valid. Except that this won’t be bound to a new object, but the global object. Meaning you risk clobbering global variables. Oops. This is definitely not what you want, and JavaScript won’t alert you to the error.

Are you sure?

6. Semicolon insertion

JavaScript will insert semicolons in places that it thinks you meant them. But it will invariably insert them where you didn’t mean for them to be. You’ll end up tearing your hair out chasing down errors.

return
{
  path: '/tmp/15.log'
};

// ERROR: returns null due to semicolon insertion on line 1!
The easiest way to avoid this issue and the rest of the issues listed on this page is to use JSLint which will catch these errors for you.

Prepare to engage the enemy

About the author

Daniel Irvine is a software craftsman at 8th Light, based in London. These days he prefers to code in Clojure and Ruby, despite having been a C++ and C# developer for the majority of his career.

For a longer bio please see danielirvine.com. To contact Daniel, send a tweet to @d_ir or use the comments section below.

Twitter GitHub Facebook Instagram dirv.me