ART - Core
ART - Shapes
Elements
Animations
Ajax
Natives
Classes
Utilities
Type: Core
Core contains a handful of common sense functions used in MooTools.
Function: typeOf
Returns the type of object that matches the item passed in.
Syntax:
typeOf(obj);
Arguments:
- obj - (object) The object to inspect.
Returns:
- 'element' - (string) If object is a DOM element node.
- 'elements' - (string) If object is a an instance of
Elements - 'textnode' - (string) If object is a DOM text node.
- 'whitespace' - (string) If object is a DOM whitespace node.
- 'arguments' - (string) If object is an arguments object.
- 'array' - (string) If object is an array.
- 'object' - (string) If object is an object.
- 'string' - (string) If object is a string.
- 'number' - (string) If object is a number.
- 'date' - (string) If object is a date.
- 'boolean' - (string) If object is a boolean.
- 'function' - (string) If object is a function.
- 'regexp' - (string) If object is a regular expression.
- 'class' - (string) If object is a Class (created with new Class, or the extend of another class).
- 'collection' - (string) If object is a native htmlelements collection, such as childNodes, getElementsByTagName, etc.
- 'window' - (string) If object is the window object.
- 'document' - (string) If object is the document object.
- 'event' - (string) If object is an event.
- 'null' - (boolean) If object is undefined, null, NaN or none of the above.
Example:
var myString = 'hello'; typeOf(myString); // returns "string".
Notes:
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.
Function: instanceOf
Checks to see if an object is an instance of a particular Type.
Syntax:
instanceOf(item, object)
Arguments:
- item - (mixed) The item which you want to check
- object - (mixed) The Type you wish to compare with
Returns:
- (boolean) Whether or not the item is an instance of the object.
Examples:
var foo = []; instanceOf(foo, Array) // returns true instanceOf(foo, String) // returns false var myClass = new Class(); var bar = new myClass(); instanceOf(bar, myClass) // returns true
Type
MooTools extends native types, like String, Array and Number to make them even more useful.
The Types MooTools uses are:
- String
- Array
- Number
- Function
- RegExp
- Date
- Boolean
Custom MooTools types are:
- Element
- Elements
- Event
Type method: implement
This method implements a new method to the Type's prototype.
Syntax:
myType.implement(name, method); // OR myType.implement(methods);
Arguments:
- name: - (string) The method name
- method: - (function) The method function
Or
- methods: - (object) An object with key-value pairs. The key is the method name, the value is the method function.
Returns:
- (object) The Type
Examples:
Array.implement('limitTop', function(top){ for (var i = 0, l = this.length; i < l; i++){ if (this[i] > top) this[i] = top; } return this; }); // which we now can use as: [1, 2, 3, 4, 5, 6].limitTop(4); // returns [1, 2, 3, 4, 4, 4]; // It is also possible to pass an object of methods String.implement({ repeat: function(times){ var string = ''; while (times--) string += this; return string; }, ftw: function(){ return this + ' FTW!'; } }); // which we now can use as: 'moo! '.repeat(3); // returns 'moo! moo! moo!' 'MooTools'.ftw(); // returns 'MooTools FTW!' // or combined ('MooTools'.ftw() + ' ').repeat(2); // returns 'MooTools FTW! MooTools FTW!'
Type method: extend
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.
Syntax:
myType.extend(name, method); // OR myType.extend(methods);
Arguments:
- name: - (string) The method name
- method: - (function) The function
Or
- methods: - (object) An object with key-value pairs. The key is the method name, the value is the function.
Returns:
- (object) The Type
Examples:
RegExp.extend('from', function(regexp, flags){ return new RegExp(regexp, flags); }); Number.extend('parseCurrency', function(currency){ // takes a string and transforms it into a number to // do certain calculations });
Generics
Most methods of types can be used as generic functions. These are the already existing JavaScript methods, methods MooTools adds, or methods you implemented yourself. It becomes more clear in the following example.
Example:
var everyArgBiggerThanTwo = function(){ // Instead of this return Array.prototype.every.call(arguments, someFunction); // we can use return Array.every(arguments, someFunction); };
This is useful if methods of a certain type should be used as function of another type.
As the example above it is used for the Arguments type, which is not an real array, so
arguments.every(fn) would not work however Array.every(arguments, fn) does work in
MooTools.
Syntax:
Type.methodName(thisArg[, arg1, arg2, ...]);
Arguments:
- thisArg - (mixed) This is the subject, which is usually
thisArg.method([arg1, arg2, ...]); - arg1, arg2, ... - (mixed) Additional arguments which will be passed as method arguments
Returns:
- (mixed) anything the method usually returns
Deprecated Functions
stuff
Function: $chk
This method has been deprecated and will have no equivalent in MooTools 1.3.
If you really need this function you can implement it like so:
Example:
var $chk = function(obj){ return !!(obj || obj === 0); };
Function: $clear
This method has been deprecated. Please use clearInterval or clearTimeout instead.
See Also:
Function: $defined
This method has been deprecated.
If you really need this function you can implement it like so:
Example:
var $defined = function(obj){ return (obj != undefined); }; // or just use it like this: if(obj != undefined){ // do something }
Function: $arguments
This method has been deprecated and will have no equivalent in MooTools 1.3.
If you really need this function you can implement it like so:
Example:
var $arguments = function(i){ return function(){ return arguments[i]; }; };
Function: $empty
This method has been deprecated. Use Function.from instead.
Example:
var myFunc = Function.from(); // or better: var myFunc = function(){};
Function: $lambda
This method has been deprecated. Use Function.from instead.
Example:
myLink.addEvent('click', Function.from(false)); // prevents a link Element from being clickable
Function: $extend
This method has been deprecated. Please use Object.append instead.
Function: $merge
This method has been deprecated. Please use Object.merge instead.
Function: $each
This method has been deprecated. Please use Array.each or Object.each instead.
Function: $pick
This method has been deprecated. Please use Array.pick instead.
Function: $random
This method has been deprecated. Please use Number.random instead.
Function: $splat
This method has been deprecated. Please use Array.from instead.
Function: $time
This method has been deprecated. Please use Date.now() instead.
Syntax:
var time = Date.now();
Returns:
- (number) - The current timestamp.
Function: $try
This method has been deprecated. Please use Function.attempt instead.
Function: $type
This method has been deprecated. Please use typeOf instead.