A couple of years ago, JavaScript was generally looked down on as something that one did when one couldn’t be bothered doing proper programming. The advent of Ajax has given the language a smart new set of clothes, and it’s almost respectable to describe oneself as a JavaScript programmer. I’ve followed this transition myself, from doing JavaScript here and there because I had to, to figuring out some fairly neat tricks that just couldn’t be accomplished any other way.
I even started to dig into the language itself, and was pleasantly surprised to find out that, although it wasn’t really that much like Java at all, it had a style and logic of its own. I got my head around prototype chains and started writing JavaScript objects. I was very pleased with myself the day I figured out how closures worked in JavaScript, and congratulated myself on achieving a certain level of mastery. Pride comes before the fall.
I picked up Sam Stephenson’s Prototype library somewhere along the way, and copied a few lines of code written by someone else who had used it, and found that it provided shortcuts that speeded up my typing. It wasn’t long before curiosity got the better of me and I wanted to know how it worked. When I first read the source code for Prototype, I thought I’d stumbled across some arcane dialect of Perl. This didn’t look like the JavaScript that I was accustomed to writing, and I did the only thing a brave and seasoned programmer could do—I ran away and hid.
I couldn’t keep away for long, though, and each time I looked, the coding style made more sense. I was kicking the tires of this new scripting language called Ruby at the same time and I started to make a few connections between the two, and gradually a pattern began to emerge. Along the way, I realized that Sam had forgotten more about closures, prototype chains, and all the other things that make JavaScript such a fun language, than I had learned in the first place. A lot of thought had gone into building up the library the way it was, so that a few well-placed lines of code early on could be exploited again and again elsewhere.
Thomas Fuchs’s Scriptaculous library takes the same approach, building larger constructs out of well-designed parts, and then, with a flourish, wraps the whole thing up so that the novice can exploit all the power and grace of these libraries in a few lines of code. When I teach Ajax courses, the day on which we unwrap Scriptaculous and create singing, dancing, drag-and-dropping interactive user interfaces (OK, so I lied about the singing and dancing) often feels a bit like Christmas, as my students achieve things with a few lines of code and a couple of hours that look slicker and smarter than what they thought they could turn out in a week.
You can use this book to find out how to harness the power of these libraries in a few lines of code, quickly. Prototype and Scriptaculous let you do the easy things easily, so that you can get on with the business side of your work, unencumbered by cross-browser worries or the burden of supporting your own libraries. You can also use the book to develop a deeper understanding of how these libraries work, and, as a consequence, how JavaScript works.
For me, using Prototype and Scriptaculous has made JavaScript coding more fun. I hope that fun is catching. Enjoy.
Dave Crane