enter the pirate
A poorly maintained blog of technologyArchive for May, 2008
Creating Classes in Prototype, MooTools
I come from the object oriented world, as I’m sure many of you do. Creating a class is a great way to write portable code that is easy to understand, and easy for other developers to work with.
This is just a brief overview of how two of the more popular JavaScript frameworks implement classes. Prototype v.1.6.0.2 and MooTools v.1.11 is assumed. This is a class used in an application that is passing data back to server-side code via the ASP.NET ClientScript.RegisterClientScriptBlock functionality.
MooTools
Let’s create a new class, and add a method to the class that we’ll use later in a project.
var Filter = new Class({
initialize: function(type, value) {
this.type = type;
this.value = value;
}
});Now let’s add our method for the class.
Filter.implement({
toReq: function() {
return this.type + ':' + this.value;
}
});
I’m using MooTools in the live application, and I’m very happy with the performance. Now let’s take a look at how to do the same thing in Prototype.
Prototype
With Prototype, we’ll create our class and it’s constructor as well as our method in the same block of code.
var Filter = Class.create({
initialize: function(type, value) {
this.type = type;
this.value = value;
},
toReq: function() {
return this.type + ':' + this.value;
}
});
The preference is entirely up to the programmer and their background, personal experience.
Prototype JavaScript framework
I’ve made up my mind (at least until tomorrow). The Prototype JavaScript framework is by far my favorite JS framework. It’s not the lightest framework available, but it makes up for that with functionality.
I’ve spent the past few days comparing mootools 1.11 with Prototype 1.6.0.2, and while I like the effects of mootools, from a programming standpoint I prefer Prototype.
The online documentation for Prototype is outstanding (They also have a downloadable PDF and CHM version of the documentation). The code for creating classes is logical and fully featured. One thing that impressed me in comparing mootools to Prototype was that Prototype supports class creation as well as inheritance, and overriding, whereas mootools does not.
The Perfect Solution
Perfect might be a stretch; but, from what I can tell it’s pretty close. moo.fx (by the creator of mootools), is a super-lightweight JavaScript effects framework that can be used in combination with Prototype. Using the two frameworks together would allow you to harness the robust functionality of Prototype, and the rich interface effects of mootools.