andrew burke





Prototype Library and JavaScript

Posted on: 2007-06-26

The Prototype JavaScript libraries have been built into Ruby on Rails since I started working with it in 2005, and I had seen some interesting things done with it, but haven't really done much with it myself. Part of this is becuase, in typical Web 2.0 fashion the documentation is kind of sparse and doesn't really cover what I want to do. Thankfully (and also in typical web 2.0 fashion) the Pragmatic Programmers have a book in beta called Prototype and I bought it last week as a PDF, and by the time I had read half of it a day later, I was building a nice fast dynamic reporting framework for my big Get Signage project.

I find that poking around raw APIs - especially if I'm busy trying to accomplish something else - doesn't often give me enough to figure out what's interesting about a technology. All I need is a few good examples and some explanations and then I'm on my way - and the Pragmatic book starts with the good examples right up front.

It is a beta version of the book, and it still has some typos and funny code examples, and you can tell that Christophe Porteneuve's mother tongue is French with some of the phrasing and word usage - but nothing gets in the way of the raw usefulness of the book.

Reading this led me to reflect on JavaScript's odd history. The language actually started out as LiveScript, but that wasn't sexy enough for mid-90s Netscape, who renamed it JavaScript, since Java was the big huge deal at the time. JavaScript also acquired a lot of cruft and wordiness from its Java namesake to make it seem more respectable.

Over the last few years, dynamically typed 'scripting' languages have come into their own - the simplicity and elegance of Ruby and all the 'P' languages (well maybe not elegance for PHP) has come to be seen as a good thing - especially for smaller-scale things like web pages. People have started to notice that underneath all of the Java-style cruft there is a nice dynamic language which is like Self or Ruby. Prototype cleans out the plumbing of JavaScript and turn it back into a Ruby-style scripting language - and now it's much easier to work with.

The biggest deal for me turns out to be in many ways the simplest: the replacement of "document.getElementById('table-contents')" with "$('table-contents')", and a pile of element-specific value retrievers with $F('firstname'). Suddenly, getting stuff out of the DOM isn't a bloated headache, and the size of my code has gone way down. It's interesting how it's taken about a decade for people to figure out where a language needs to be optimized.

Previous: Godin LG Hmb - my new guitar
Next: ... that creepy ass botox-phenomenon