3 My Object Oriented javascript.
9 Valerio Proietti, <http://mad4milk.net>
15 - Class is slightly based on Base.js <http://dean.edwards.name/weblog/2006/03/base/> (c) 2006 Dean Edwards, License <http://creativecommons.org/licenses/LGPL/2.1/>
16 - Some functions are based on those found in prototype.js <http://prototype.conio.net/> (c) 2005 Sam Stephenson sam [at] conio [dot] net, MIT-style license
17 - Documentation by Aaron Newton (aaron.newton [at] cnet [dot] com) and Valerio Proietti.
22 The base class object of the <http://mootools.net> framework.
25 properties - the collection of properties that apply to the class. Creates a new class, its initialize method will fire upon class instantiation.
28 >var Cat = new Class({
29 > initialize: function(name){
33 >var myCat = new Cat('Micia');
34 >alert myCat.name; //alerts 'Micia'
37 var Class = function(properties){
38 var klass = function(){
40 if (this[p]) this[p]._proto_ = this;
42 if (arguments[0] != 'noinit' && this.initialize) return this.initialize.apply(this, arguments);
44 klass.extend = this.extend;
45 klass.implement = this.implement;
46 klass.prototype = properties;
52 Returns an empty function
55 Class.empty = function(){};
59 same as new Class. see <Class>
62 Class.create = function(properties){
63 return new Class(properties);
70 Returns the copy of the Class extended with the passed in properties.
73 properties - the properties to add to the base class in this new Class.
76 >var Animal = new Class({
77 > initialize: function(age){
81 >var Cat = Animal.extend({
82 > initialize: function(name, age){
83 > this.parent(age); //will call the previous initialize;
87 >var myCat = new Cat('Micia', 20);
88 >alert myCat.name; //alerts 'Micia'
89 >alert myCat.age; //alerts 20
92 extend: function(properties){
93 var pr0t0typ3 = new this('noinit');
94 for (var property in properties){
95 var previous = pr0t0typ3[property];
96 var current = properties[property];
97 if (previous && previous != current) current = previous.parentize(current) || current;
98 pr0t0typ3[property] = current;
100 return new Class(pr0t0typ3);
105 Implements the passed in properties to the base Class prototypes, altering the base class, unlike <Class.extend>.
108 properties - the properties to add to the base class.
111 >var Animal = new Class({
112 > initialize: function(age){
117 > setName: function(name){
121 >var myAnimal = new Animal(20);
122 >myAnimal.setName('Micia');
123 >alert(myAnimal.name); //alerts 'Micia'
126 implement: function(properties){
127 for (var property in properties) this.prototype[property] = properties[property];
132 /* Section: Object related Functions */
135 Function: Object.extend
136 Copies all the properties from the second passed object to the first passed Object.
137 If you do myWhatever.extend = Object.extend the first parameter will become myWhatever, and your extend function will only need one parameter.
147 > 'lastName': 'Dorian'
149 >Object.extend(firstOb, secondOb);
150 >//firstOb will become:
153 > 'lastName': 'Dorian',
159 The first object, extended.
162 Object.extend = function(){
163 var args = arguments;
164 if (args[1]) args = [args[0], args[1]];
165 else args = [this, args[0]];
166 for (var property in args[1]) args[0][property] = args[1][property];
171 Function: Object.Native
172 Will add a .extend method to the objects passed as a parameter, equivalent to <Class.implement>
175 a number of classes/native javascript objects
179 Object.Native = function(){
180 for (var i = 0; i < arguments.length; i++) arguments[i].extend = Class.prototype.implement;
183 new Object.Native(Function, Array, String, Number);
187 parentize: function(current){
190 this.parent = previous;
191 return current.apply(this, arguments);