reimporting, phase 2
[mootools.git] / Moo.js
blob24ba0e09640a9043de542eded403568ec8b3d032
1 /*
2 Script: Moo.js
3         My Object Oriented javascript.
4         
5 Dependancies:
6          Has no dependancies.
7         
8 Author:
9         Valerio Proietti, <http://mad4milk.net>
11 License:
12         MIT-style license.
14 Credits:
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.
21 Class: Class
22         The base class object of the <http://mootools.net> framework.
23         
24 Arguments:
25         properties - the collection of properties that apply to the class. Creates a new class, its initialize method will fire upon class instantiation.
26         
27 Example:
28         >var Cat = new Class({
29         >       initialize: function(name){
30         >               this.name = name;
31         >       }
32         >});
33         >var myCat = new Cat('Micia');
34         >alert myCat.name; //alerts 'Micia'
37 var Class = function(properties){
38         var klass = function(){
39                 for (var p in this){
40                         if (this[p]) this[p]._proto_ = this;
41                 }
42                 if (arguments[0] != 'noinit' && this.initialize) return this.initialize.apply(this, arguments);
43         };
44         klass.extend = this.extend;
45         klass.implement = this.implement;
46         klass.prototype = properties;
47         return klass;
51 Property: empty
52         Returns an empty function
55 Class.empty = function(){};
58 Property: create
59         same as new Class. see <Class>
62 Class.create = function(properties){
63         return new Class(properties);
66 Class.prototype = {
68         /*
69         Property: extend
70                 Returns the copy of the Class extended with the passed in properties.
71                 
72         Arguments:
73                 properties - the properties to add to the base class in this new Class.
74                 
75         Example:
76                 >var Animal = new Class({
77                 >       initialize: function(age){
78                 >               this.age = age;
79                 >       }
80                 >});
81                 >var Cat = Animal.extend({
82                 >       initialize: function(name, age){
83                 >               this.parent(age); //will call the previous initialize;
84                 >               this.name = name;
85                 >       }
86                 >});
87                 >var myCat = new Cat('Micia', 20);
88                 >alert myCat.name; //alerts 'Micia'
89                 >alert myCat.age; //alerts 20
90         */
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;
99                 }
100                 return new Class(pr0t0typ3);
101         },
102         
103         /*      
104         Property: implement
105                 Implements the passed in properties to the base Class prototypes, altering the base class, unlike <Class.extend>.
107         Arguments:
108                 properties - the properties to add to the base class.
109                 
110         Example:
111                 >var Animal = new Class({
112                 >       initialize: function(age){
113                 >               this.age = age;
114                 >       }
115                 >});
116                 >Animal.implement({
117                 >       setName: function(name){
118                 >               this.name = name
119                 >       }
120                 >});
121                 >var myAnimal = new Animal(20);
122                 >myAnimal.setName('Micia');
123                 >alert(myAnimal.name); //alerts 'Micia'
124         */
125         
126         implement: function(properties){
127                 for (var property in properties) this.prototype[property] = properties[property];
128         }
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.
138                 
139 Example:
140         >var firstOb = {
141         >       'name': 'John',
142         >       'lastName': 'Doe'
143         >};
144         >var secondOb = {
145         >       'age': '20',
146         >       'sex': 'male',
147         >       'lastName': 'Dorian'
148         >};
149         >Object.extend(firstOb, secondOb);
150         >//firstOb will become: 
151         >{
152         >       'name': 'John',
153         >       'lastName': 'Dorian',
154         >       'age': '20',
155         >       'sex': 'male'
156         >};
157         
158 Returns:
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];
167         return args[0];
171 Function: Object.Native
172         Will add a .extend method to the objects passed as a parameter, equivalent to <Class.implement>
174 Arguments:
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);
185 Function.extend({
187         parentize: function(current){
188                 var previous = this;
189                 return function(){
190                         this.parent = previous;
191                         return current.apply(this, arguments);
192                 };
193         }