Rename <Type>.from to <Type>.convert
[mootools.git] / Docs / Core / Core.md
blobef1a06f0ae94ba91780c2dd3f2a7dccd31b49a3a
1 # Type: Core {#Core}
3 Core contains common functions used in [MooTools][].
5 ## Function: typeOf {#Core:typeOf}
7 Returns the type of an object.
9 ### Syntax:
11         typeOf(obj);
13 ### Arguments:
15 1. obj - (*object*) The object to inspect.
17 ### Returns:
19 * 'element'    - (*string*) If object is a DOM element node.
20 * 'elements'   - (*string*) If object is an instance of [Elements][].
21 * 'textnode'   - (*string*) If object is a DOM text node.
22 * 'whitespace' - (*string*) If object is a DOM whitespace node.
23 * 'arguments'  - (*string*) If object is an arguments object.
24 * 'array'      - (*string*) If object is an array.
25 * 'object'     - (*string*) If object is an object.
26 * 'string'     - (*string*) If object is a string.
27 * 'number'     - (*string*) If object is a number.
28 * 'date'       - (*string*) If object is a date.
29 * 'boolean'    - (*string*) If object is a boolean.
30 * 'function'   - (*string*) If object is a function.
31 * 'regexp'     - (*string*) If object is a regular expression.
32 * 'class'      - (*string*) If object is a [Class][] (created with new Class or the extend of another class).
33 * 'collection' - (*string*) If object is a native HTML elements collection, such as childNodes or getElementsByTagName.
34 * 'window'     - (*string*) If object is the window object.
35 * 'document'   - (*string*) If object is the document object.
36 * 'domevent'   - (*string*) If object is an event.
37 * 'null'       - (*string*) If object is undefined, null, NaN or none of the above.
39 ### Example:
41         var myString = 'hello';
42         typeOf(myString); // returns "string"
44 ### Note:
46 This method is equivalent to *$type* from MooTools 1.2, with the exception that undefined and null values now return 'null' as a string, instead of false.
48 ## Function: instanceOf {#Core:instanceOf}
50 Checks if an object is an instance of a particular type.
52 ### Syntax:
54         instanceOf(item, object)
56 ### Arguments:
58 1. item - (*mixed*) The item to check.
59 2. object - (*mixed*) The type to compare it with.
61 ### Returns:
63 * (*boolean*) Whether or not the item is an instance of the object.
65 ### Examples:
67         var foo = [];
68         instanceOf(foo, Array) // returns true
69         instanceOf(foo, String) // returns false
71         var myClass = new Class();
72         var bar = new myClass();
73         instanceOf(bar, myClass) // returns true
75 ### Type {#Type}
77 MooTools extends native types, like string, array or number to make them even more useful.
79 The types MooTools uses are:
81 - String
82 - Array
83 - Number
84 - Function
85 - RegExp
86 - Date
87 - Boolean
89 Custom MooTools types are:
91 - Element
92 - Elements
93 - Event
95 ## Type method: implement {#Type:implement}
97 This method implements a new method to the type's prototype.
99 ### Syntax:
101         myType.implement(name, method);
103 **or**
105         myType.implement(methods);
107 ### Arguments:
109 1. name - (*string*) The method name.
110 2. method - (*function*) The method function.
112 **or**
114 1. methods - (*object*) An object with key-value pairs. The key is the method name, the value is the method function.
116 ### Returns:
118 * (*object*) The type.
120 ### Examples:
122         Array.implement('limitTop', function(top){
123                 for (var i = 0, l = this.length; i < l; i++){
124                         if (this[i] > top) this[i] = top;
125                 }
126                 return this;
127         });
129         [1, 2, 3, 4, 5, 6].limitTop(4); // returns [1, 2, 3, 4, 4, 4]
131 It is also possible to pass an object of methods:
133         String.implement({
134                 repeat: function(times){
135                         var string = '';
136                         while (times--) string += this;
137                         return string;
138                 },
139                 ftw: function(){
140                         return this + ' FTW!';
141                 }
142         });
144         'moo! '.repeat(3); // returns "moo! moo! moo! "
145         'MooTools'.ftw(); // returns "MooTools FTW!"
146         ('MooTools'.ftw() + ' ').repeat(2); // returns "MooTools FTW! MooTools FTW! "
148 ## Type method: extend {#Type:extend}
150 Adds one or more functions to the type. These are static functions that accept for example other types to parse them into the type, or other utility functions that belong to the certain type.
152 ### Syntax:
154         myType.extend(name, method);
156 **or**
158         myType.extend(methods);
160 ### Arguments:
162 1. name - (*string*) The method name.
163 2. method - (*function*) The method function.
165 **or**
167 1. methods - (*object*) An object with key-value pairs. The key is the method name, the value is the method function.
169 ### Returns:
171 * (*object*) The type.
173 ### Examples:
175         RegExp.extend('from', function(regexp, flags){
176                 return new RegExp(regexp, flags);
177         });
179         Number.extend('parseCurrency', function(currency){
180                 // takes a string and transforms it into a number to
181                 // do certain calculations
182         });
184 ## Generics {#Type:generics}
186 Most methods of types can be used as generic functions. These are the already existing JavaScript methods, methods MooTools adds, or methods you [implemented][implement] yourself.
188 ### Example:
190         var everyArgBiggerThanTwo = function(){
191                 // Instead of this
192                 return Array.prototype.every.call(arguments, someFunction);
193                 // you can use
194                 return Array.every(arguments, someFunction);
195         };
197 This is useful if methods of a certain type should be used as function of another type.
198  As the example above, it is used for the Arguments type, which is not a real array, so `arguments.every(fn)` would not work. However, `Array.every(arguments, fn)` does work in MooTools.
200 ### Syntax:
202         Type.methodName(thisArg[, arg1, arg2, ...]);
204 ### Arguments:
206 1. thisArg - (*mixed*) This is the subject, which is usually `thisArg.method([arg1, arg2, ...]);`.
207 2. arg1, arg2, ... - (*mixed*) Additional arguments which will be passed as method arguments.
209 ### Returns:
211 - (*mixed*) Anything the method usually returns.
213 [Class]: /core/Class/Class
214 [Elements]: /core/Element/Element
215 [implement]: core/Core/Core#Type:implement
216 [MooTools]: http://mootools.net
220 Deprecated Functions {#Deprecated-Functions}
221 ============================================
224 Function: $chk {#Deprecated-Functions:chk}
225 ---------------------
227 This method has been deprecated and will have no equivalent in MooTools 1.3.
229 If you really need this function you can implement it like so:
231 ### Example:
233         var $chk = function(obj){
234                 return !!(obj || obj === 0);
235         };
239 Function: $clear {#Deprecated-Functions:clear}
240 -------------------------
242 This method has been deprecated. Please use *clearInterval* or *clearTimeout* instead.
244 ### See Also:
246 - [MDN clearTimeout][], [MDN clearInterval][]
249 Function: $defined {#Deprecated-Functions:defined}
250 -----------------------------
252 This method has been deprecated.
254 If you really need this function you can implement it like so:
256 ### Example:
258         var $defined = function(obj){
259                 return (obj != undefined);
260         };
262         // or just use it like this:
263         if(obj != undefined){
264                 // do something
265         }
268 Function: $arguments {#Deprecated-Functions:arguments}
269 ---------------------------------
271 This method has been deprecated and will have no equivalent in MooTools 1.3.
273 If you really need this function you can implement it like so:
275 ### Example:
277         var $arguments = function(i){
278                 return function(){
279                         return arguments[i];
280                 };
281         };
285 Function: $empty {#Deprecated-Functions:empty}
286 -------------------------
288 This method has been deprecated. Use [Function.convert](/core/Types/Function#Function:Function:convert) instead.
290 ### Example:
292         var myFunc = Function.convert();
293         // or better:
294         var myFunc = function(){};
298 Function: $lambda {#Deprecated-Functions:lambda}
299 ---------------------------
301 This method has been deprecated. Use [Function.convert](/core/Types/Function#Function:Function:convert) instead.
303 ### Example:
305         myLink.addEvent('click', Function.convert(false)); // prevents a link Element from being clickable
309 Function: $extend {#Deprecated-Functions:extend}
310 ---------------------------
312 This method has been deprecated. Please use [Object.append](/core/Types/Object#Object:Object-append) instead.
316 Function: $merge {#Deprecated-Functions:merge}
317 -------------------------
319 This method has been deprecated. Please use [Object.merge](/core/Types/Object#Object:Object-merge) instead.
323 Function: $each {#Deprecated-Functions:each}
324 -----------------------
326 This method has been deprecated. Please use [Array.each](/core/Types/Array#Array:Array-each) or [Object.each](/core/Types/Object#Object:Object-each) instead.
330 Function: $pick {#Deprecated-Functions:pick}
331 -----------------------
333 This method has been deprecated. Please use [Array.pick](/core/Types/Array#Array:pick) instead.
337 Function: $random {#Deprecated-Functions:random}
338 ---------------------------
340 This method has been deprecated. Please use [Number.random](/core/Types/Number#Number:Number-random) instead.
344 Function: $splat {#Deprecated-Functions:splat}
345 -------------------------
347 This method has been deprecated. Please use [Array.convert](/core/Types/Array#Array:Array:convert) instead.
348 However `$splat` does *not* transform Array-like objects such as NodeList or FileList in arrays, `Array.convert` does.
351 Function: $time {#Deprecated-Functions:time}
352 -----------------------
354 This method has been deprecated. Please use *Date.now()* instead.
356 ### Syntax:
358         var time = Date.now();
360 ### Returns:
362 * (*number*) - The current timestamp.
366 Function: $try {#Deprecated-Functions:try}
367 ---------------------
369 This method has been deprecated. Please use [Function.attempt](/core/Types/Function#Function:Function-attempt) instead.
373 Function: $type {#Deprecated-Functions:type}
374 -----------------------
376 This method has been deprecated. Please use [typeOf](#Core:typeOf) instead.
380 [Array]: /core/Types/Array
381 [Function:bind]: /core/Types/Function/#bind
382 [Function:delay]: /core/Types/Function/#delay
383 [Function:periodical]: /core/Types/Function/#periodical
384 [MDN clearInterval]: https://developer.mozilla.org/en/DOM/window.clearInterval
385 [MDN clearTimeout]: https://developer.mozilla.org/en/DOM/window.clearTimeout