XML.com 
 Published on XML.com http://www.xml.com/pub/a/2007/01/24/whats-new-in-prototype-15.html
See this if you're having trouble printing code examples

 

What's New in Prototype 1.5?
By Scott Raymond
January 24, 2007

The latest release of Ruby on Rails, version 1.2, was announced last week to great fanfare. But the announcement might have overshadowed news of a simultaneous release: version 1.5 of Prototype, the popular JavaScript library. Despite the synchronization and developer overlap between the two projects, nothing about Prototype depends on Rails—it's perfectly suitable for use with any server-side technology. In fact, Prototype has amassed a huge user base beyond the Rails community—from dozens of Web 2.0 startups to household names like Apple, NBC, and Gucci.

The Prototype library is fairly compact (about 15K), and decidedly not a kitchen-sink library. It doesn't provide custom widgets or elaborate visual effects. Instead, it just strives to make JavaScript more pleasant to work with. In many ways, Prototype acts like the missing standard library for JavaScript—it provides the functionality that arguably ought to be part of the core language.

Despite the minor version number bump, the 1.5 release is a major one. It's been over a year since 1.4 was released, and the library has made significant strides in that time, while retaining complete backward compatibility (with one notable exception, but more on that later). In this article, we'll look at what's new, organized into four major areas: Ajax support, String extensions, Array/Enumerable extensions, and DOM access.

For an introduction to some of Prototype's capabilities, see Prototype: Easing AJAX's Pain and The Power of Prototype.js. To get a feel for the breadth of the library, peruse the new, long-awaited API documentation on the new, long-awaited Prototype website. Or check out my book, Ajax on Rails, which includes an exhaustive reference to Prototype, as well as its companion library script.aculo.us. The Prototype reference is also available as a PDF: Prototype Quick Reference.

Ajax Support

Prototype is perhaps best known for its top-notch Ajax support. Of course, Ajax-style interactions can be created without a JavaScript library, but the process can be fairly verbose and error-prone. Prototype makes Ajax development more accessible by accounting for the varieties of browser implementations and providing a clear, natural API. The 1.5 release adds even more power, especially as relates to creating RESTful, HTTP-embracing requests. Prototype now has the ability to easily access HTTP request and response headers and simulate HTTP methods other than GET and POST by tunneling those requests over POST. Specifically:

String Extensions

In a typical web application, a great deal of code is written to simply manipulate strings. Thus, a thorough set of string-processing methods are an invaluable weapon in the web developer's arsenal. With version 1.5, Prototype's suite of extensions to the standard String class (or more accurately, the String prototype) has roughly doubled. Here are the latest additions.

In addition to Prototype's new extensions to the String prototype, it also defines an entirely new class for string manipulation: Template, which provides simple templating functionality with JavaScript strings. Using the Template class is simple: just instantiate a new template with the constructor, and then call evaluate on the instance, providing the data to be interpolated. For example:

var row = new Template('<tr><td>#{name}</td><td>#{age}</td></tr>');

To render a template, call evaluate on it, passing an object containing the needed data. For example:

var person = { name: 'Sam', age: 21 };
row.evaluate(person); // => '<tr><td>Sam</td><td>21</td></tr>'
row.evaluate({})); // => '<tr><td></td><td></td></tr>'

The default template syntax mimics Ruby's style of variable interpolation (e.g., #{age}). To override this behavior, provide a regular expression as the second argument to the constructor. For example:

// Using a custom pattern mimicking PHP syntax
Template.PhpPattern = /(^|.|\r|\n)(<\?=\s*\$(.*?)\s*\?>)/;
var row = new Template('<tr><td><?= $name ?></td><td><?= $age ?></td></tr>', Template.PhpPattern);
row.evaluate({ name: 'Sam', age: 21 }); // "<tr><td>Sam</td><td>21</td></tr>"

Templates are especially powerful in combination with Prototype's capability to insert content into the DOM. For example:

// <table id="people" border="1"></table>
var row = new Template('<tr><td>#{name}</td><td>#{age}</td></tr>');
var people = [{name: 'Sam', age: 21}, {name: 'Marcel', age: 27}];
people.each(function(person){
  new Insertion.Bottom('people', row.evaluate(person));
});

Array and Enumerable Extensions

The String prototype isn't the only language-native object that Prototype enhances. It also extends JavaScript's Array prototype with over a dozen methods, including four in the latest release.

In addition to the extensions directly to Array, Prototype also provides an object called Enumerable, inspired by the Ruby module of the same name. The methods defined in Enumerable are added to several type of collections, including Array, Hash, and ObjectRange. As with Ruby's Enumerable, it's possible to "mix-in" Prototype's Enumerable methods into your own custom classes as well. There are a handful of new features added in the 1.5 release:

DOM Access

The area that has gotten the most attention in the 1.5 release is Prototype's DOM access and manipulation methods.

First, a new Selector class has been added for matching elements by CSS selector tokens. The new $$() function provides easy access to the feature, returning DOM elements that match simple CSS selector strings. For example:

// Find all <img> elements inside <p> elements with class "summary", all inside the <div> with id "page". Hide each matched <img> tag:
$$('div#page p.summary img').each(Element.hide)
// Supports attribute selectors:
$$('form#foo input[type=text]').each(function(input) {
  input.setStyle({color: 'red'});
});

Support Insertion.Before and Insertion.After for <tr> elements in IE.

Add Element.extend, which mixes Element methods into a single HTML element. This means you can now write $('foo').show() instead of Element.show('foo'). $(), $$() and document.getElementsByClassName() automatically call Element.extend on any returned elements. Plus, all destructive Element methods (i.e., those methods that change the element rather than return some value) now return the element itself—meaning that Element methods can be chained together. For example:

$("sidebar").addClassName("selected").show();

The 1.5 release brought a ton of new methods to Element.Methods: