Rename <Type>.from to <Type>.convert
[mootools.git] / Docs / Class / Class.md
blob0a53e8827fc66de8a614a1e16fe26be2cda4ccaf
1 Class {#Class}
2 ======================
4 The base Class of the [MooTools](http://mootools.net/) framework.
6 Class Method: constructor {#Class:constructor}
7 ----------------------------------------------
9 ### Syntax:
11         var MyClass = new Class(properties);
13 ### Arguments:
15 1. properties - Can be one of the following types:
16         * (*object*) The collection of properties that apply to the Class. Also accepts some special properties such as Extends, Implements, and initialize (see below).
17         * (*function*) The initialize function (see below).
19 #### Property: Extends
21 * (*class*) The Class that this class will extend.
23 The methods of this Class that have the same name as the Extends Class, will have a parent property, that allows you to call the other overridden method.
24 The Extends property should be the first property in a class definition.
26 #### Property: Implements
28 * (*class*)  The properties of a passed Class will be copied into the target Class.
29 * (*array*)  An array of Classes, the properties of which will be copied into this Class.
31 Implements is similar to Extends, except that it adopts properties from one or more other classes without inheritance.
32 Useful when implementing a default set of properties in multiple Classes. The Implements property should come after Extends but before all other properties.
34 #### Property: initialize
36 * (*function*) The initialize function will be the constructor for this class when new instances are created.
38 #### Property: toElement
40 * (*function*) A method which returns an element. This method will be automatically called when passing an instance of a class in the [document.id][] function.
43 ### Returns:
45 * (*class*) The created Class.
47 ### Examples:
49 #### Class Example:
51         var Cat = new Class({
52                 initialize: function(name){
53                         this.name = name;
54                 }
55         });
56         var myCat = new Cat('Micia');
57         alert(myCat.name); // alerts 'Micia'
59         var Cow = new Class({
60                 initialize: function(){
61                         alert('moooo');
62                 }
63         });
65 #### Extends Example:
67         var Animal = new Class({
68                 initialize: function(age){
69                         this.age = age;
70                 }
71         });
72         var Cat = new Class({
73                 Extends: Animal,
74                 initialize: function(name, age){
75                         this.parent(age); // calls initalize method of Animal class
76                         this.name = name;
77                 }
78         });
79         var myCat = new Cat('Micia', 20);
80         alert(myCat.name); // alerts 'Micia'.
81         alert(myCat.age); // alerts 20.
83 #### Implements Example:
85         var Animal = new Class({
86                 initialize: function(age){
87                         this.age = age;
88                 }
89         });
90         var Cat = new Class({
91                 Implements: Animal,
92                 setName: function(name){
93                         this.name = name
94                 }
95         });
96         var myAnimal = new Cat(20);
97         myAnimal.setName('Micia');
98         alert(myAnimal.name); // alerts 'Micia'.
103 Class Method: implement {#Class:implement}
104 ------------------------------------------
106 Implements the passed in properties into the base Class prototypes, altering the base Class.
107 The same as creating a [new Class](#Class:constructor) with the Implements property, but handy when you need to modify existing classes.
109 ### Syntax:
111         MyClass.implement(properties);
113 ### Arguments:
115 1. properties - (*object*) The properties to add to the base Class.
117 ### Examples:
119         var Animal = new Class({
120                 initialize: function(age){
121                         this.age = age;
122                 }
123         });
124         Animal.implement({
125                 setName: function(name){
126                         this.name = name;
127                 }
128         });
129         var myAnimal = new Animal(20);
130         myAnimal.setName('Micia');
131         alert(myAnimal.name); // alerts 'Micia'
133 [document.id]: /core/Element/Element#Window:document-id