This chapter demonstrates about the Prototype.js Class previous versions of prototypes came with basic support of class and following are the concepts covered in this chapter.
Defining Classes
Special Class Properties
Defining Classes
Description
User can create the class by using the class.create() in previous versions of prototypes came with the basic support of class creation but now the latest feature of class is automatically initialize the constructor. In prototype 1.6.0 have a feature of backward compatibility and some new features. If user have legacy prototype code the class based code worked same as before but code doesn't have work with the prototype then user need to use Object.extend method to copy the properties. The snippet below demonstrates the legacy prototype class-based code.
[jscript]
var Person = Class.create();
Person.prototype = {
initialize: function(name) {
this.name = name;
},
say: function(message) {
return this.name + ': ' + message;
}
};
var guy = new Person('Miro');
guy.say('hi');
var Pirate = Class.create();
Pirate.prototype = Object.extend(new Person(), {
say: function(message) {
return this.name + ': ' + message + ', yarr!';
}
});
var john = new Pirate('Long John');
john.say('ahoy matey');
[/jscript]
In Prototype 1.6.0 class methods there are no any special supports user can define them in existing classes which can be shown in below snippet.
[jscript]
Pirate.allHandsOnDeck = function(n) {
var voices = [];
n.times(function(i) {
voices.push(new Pirate('Sea dog').say(i + 1));
});
return voices;
}
Pirate.allHandsOnDeck(3);
[/jscript]
If user need to define several classes at once then use Object.extend method is as shown in below.
[jscript]
Object.extend(Pirate, {
song: function(pirates) { ... },
sail: function(crew) { ... },
booze: ['grog', 'rum']
});
[/jscript]
While inherit class methods are not inherited which can be added in prototype earlier versions until copy the methods manually otherwise follow the snippet below.
[jscript]
var Captain = Class.create(Pirate, {});
// this is wrong!
Object.extend(Captain, Pirate);
[/jscript]
Special Class Properties
Description
Prototype 1.6.0 have two two special class properties previous versions of prototype doesn't have special class properties earlier version special class properties are shown below.
subclass
Which hold the reference of the sub classes.
super class
Which hold the reference of the super classes.
The code below demonstrates the using of the subclass and super class as shown below.
[jscript]
Person.superclass
// -> null
Person.subclasses.length
// -> 1
Person.subclasses.first() == Pirate
// -> true
Pirate.superclass == Person
// -> true
Captain.superclass == Pirate
// -> true
Captain.superclass == Person
// -> false
[/jscript]
User can add some methods by using the Class#addMethods which can be named as Class.extend in prototype 1.6.0 RC0. User can add the extra methods. In JavaScript native prototype inheritance which can be done by adding the methods into the prototype chain. the snippet below demonstrates the addining some extra methods.
[jscript]
var john = new Pirate('Long John');
john.sleep();
// -> ERROR: sleep is not a method
// every person should be able to sleep, not just pirates!
Person.addMethods({
sleep: function() {
return this.say('ZzZ');
}
});
john.sleep();
// -> "Long John: ZzZ, yarr!"
[/jscript]
Summary
Key Points
Class can be created by class.create() method.
Prototype 1.6.0 have backward compatibility feature.