9 Revealing JavaScript Tips
- By Matt Stephens
- October 15, 2006
Over on Ayman Hourieh’s blog, you’ll find 9 JavaScript tips you may not know. The tips start out by exploiting the language’s loose typing capability: something which makes the language deceptively powerful.
In fact, JavaScript has what you might call an understated syntax. Its dynamic typing allows you to perform some surprisingly clever tricks with very little code; e.g.: passing methods around and tacking them onto new objects; and treating a data structure as a Queue one minute and a Binary Tree the next. Of course, that same flexibility can easily land you in a whole lot of trouble, too.
Dynamic typing is both a blessing and a bane. Errors that would have been caught at compile-time in static-typed languages (such as Java and C#) slip straight through to the deployment server when you’re using JavaScript. (Same goes for Ruby, or any other dynamically typed language). And “clever code” is also something of a bugbear for many project leaders who have to maintain that frightfully clever but frankly undecipherable code left behind after the team guru has fled the company to go contracting instead.
On the plus side, though, dynamic typing is by its nature great for scripting, where there is no compilation to catch type errors anyway; and where there’s more value in knocking out a “quick n’clever” duct-tape script to bind a page’s logic together. Scripting is what JavaScript is good at – the clue is in the name, of course. It doesn’t pretend to be anything more than a scripting language; it knows its place. (Contrast this with Ruby, which has value as a scripting language but tries to pass itself off as an enterprise-strength development language and platform [link to my slightly tongue-in-cheek Ruby article, poor taste warning!] ).
Anyway, phew, got that off my chest. Back to the 9 JavaScript tips... following the dynamic typing stuff, the tips move on to a handy tip for adding assertions to your scripts (JavaScript lacks a built-in assert function, but it’s easy enough to ape).
But Ayman saves the best tip for last: using the excellent Firefox plugin FireBug to debug your Ajax applications. FireBug is just so freaking useful, it should be standard issue for anyone creating even slightly "Ajaxy" web applications.