3 Core contains common functions used in [MooTools][].
5 ## Function: typeOf {#Core:typeOf}
7 Returns the type of an object.
15 1. obj - (*object*) The object to inspect.
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.
41 var myString = 'hello';
42 typeOf(myString); // returns "string"
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.
54 instanceOf(item, object)
58 1. item - (*mixed*) The item to check.
59 2. object - (*mixed*) The type to compare it with.
63 * (*boolean*) Whether or not the item is an instance of the object.
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
77 MooTools extends native types, like string, array or number to make them even more useful.
79 The types MooTools uses are:
89 Custom MooTools types are:
95 ## Type method: implement {#Type:implement}
97 This method implements a new method to the type's prototype.
101 myType.implement(name, method);
105 myType.implement(methods);
109 1. name - (*string*) The method name.
110 2. method - (*function*) The method function.
114 1. methods - (*object*) An object with key-value pairs. The key is the method name, the value is the method function.
118 * (*object*) The type.
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;
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:
134 repeat: function(times){
136 while (times--) string += this;
140 return this + ' FTW!';
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.
154 myType.extend(name, method);
158 myType.extend(methods);
162 1. name - (*string*) The method name.
163 2. method - (*function*) The method function.
167 1. methods - (*object*) An object with key-value pairs. The key is the method name, the value is the method function.
171 * (*object*) The type.
175 RegExp.extend('from', function(regexp, flags){
176 return new RegExp(regexp, flags);
179 Number.extend('parseCurrency', function(currency){
180 // takes a string and transforms it into a number to
181 // do certain calculations
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.
190 var everyArgBiggerThanTwo = function(){
192 return Array.prototype.every.call(arguments, someFunction);
194 return Array.every(arguments, someFunction);
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.
202 Type.methodName(thisArg[, arg1, arg2, ...]);
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.
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:
233 var $chk = function(obj){
234 return !!(obj || obj === 0);
239 Function: $clear {#Deprecated-Functions:clear}
240 -------------------------
242 This method has been deprecated. Please use *clearInterval* or *clearTimeout* instead.
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:
258 var $defined = function(obj){
259 return (obj != undefined);
262 // or just use it like this:
263 if(obj != undefined){
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:
277 var $arguments = function(i){
285 Function: $empty {#Deprecated-Functions:empty}
286 -------------------------
288 This method has been deprecated. Use [Function.convert](/core/Types/Function#Function:Function:convert) instead.
292 var myFunc = Function.convert();
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.
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.
358 var time = Date.now();
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