Tech Tutorials Database
GeekArticles XML Core XML
 

The Power of Prototype.js

 
Author: oreillynet.com
Category: Core XML
Comments (0)

Computer languages evolve across an interesting number of vectors, and not always in ways that the original designers had planned. For every high level, top-down decision to implement new features and capabilities, there are interesting bits of best practices, useful libraries, and design patterns that can, subtly and sometimes not so subtly, change the course of direction of a language in critical ways.

AJAX is a good case in point - I’m in the process of writing on AJAX for a book, and occasionally I have to step out of my own preconceived notions of where the language (principally refering to JavaScript here and not the XML side) has been and look at where the language is going in terms of its own long and winding path. Certainly Ruby has been influencing things by bundling in interesting JavaScript components on the server side, but I think a more interesting case in point is the use of a set of libraries - collected together as prototype.js - that are rapidly reshaping how we use the language, especially in the context of web browsers.

The prototype.js libraries, developed by Sam Stephenson at http://prototype.conio.net/, seemed to have evolved out of the Ruby on Rails project to take on a life of its own. It includes a number of extraordinarily useful library functions and introduces the “$” as notation within JavaScript. This library now underlies many of the AJAX frameworks in use on the web, and it’s not unlikely that it will creep into the “core” implementation over time.

One of the central things that prototype.js does is define a set of additional useful objects, including a new Hash object, a new Enumerable class, ranges, an easy to use AJAX class, as well as extensions to such core classes as number, string and array. It also provides the most same and rational shortcuts to entirely too verbose methods such as getElementById.

To illustrate that last point, prototype.js defines a function called “$” … yep, that’s right - the dollar sign. Turns out that the dollar sign is in fact a valid character for names in JavaScript (a fact obscured by years of dominance by JScript, which didn’t recognize this salient fact). The prototype.js library defines $() as a function to replace the ubiquitous (and painful) document.getElementById() method, with an added twist that if an element (or other object) is passed into it, the object gets passed out the other side. This means that if you are wanting to refer to an element with id “foo”, you’d use the expression:

var foo = $("foo"); var foo2 = $(foo);

rather than

var foo = document.getElementById("foo"); if (typeof foo == "string"){ var foo2 = document.getElementById("foo"); } else { var foo2 = foo; }

Given a typical browser function, this can turn something as painful as:

var updateFunctionList=function(){ var functionMenu = document.getElementById("functionMenu"); var functionDisplay = document.getElementById("functionDisplay"); var functionTabs = document.getElementById("functionTabs"); var functionTabPanels = document.getElementById("functionTabPanels"); tabCt=0; for (key in this.functionType){ tabCt++; var tab = ""; var tabNode = (new DOMParser()).parseFromString(tab,"text/xml").documentElement.cloneNode(true); if (tabCt ==1){ tabNode.setAttribute("selected","true"); } functionTabs.appendChild(tabNode); var tabPanel = ""; var tabPanelNode = (new DOMParser()).parseFromString(tabPanel,"text/xml").documentElement.cloneNode(true); functionTabPanels.appendChild(tabPanelNode); } for (key in this.functionSet){ var menuitem = ""; menuitemNode = (new DOMParser()).parseFromString(menuitem,"text/xml").documentElement.cloneNode(true); functionMenu.appendChild(menuitemNode); var button = "