4 Core contains an handful of common sense functions used in [MooTools](http://mootools.net).
5 It also contains some basic [Hash][] and [Array][] methods.
10 Checks to see if a value exists or is 0. Useful for allowing 0.
18 1. item - (*mixed*) The item to inspect.
22 * (*boolean*) If the object passed in exists or is 0, returns true. Otherwise, returns false.
26 function myFunction(arg){
27 if($chk(arg)) alert('The object exists or is 0.');
28 else alert('The object is either null, undefined, false, or ""');
33 Function: $clear {#clear}
34 -------------------------
36 Clears a Timeout or an Interval. Useful when working with [Function:delay][] and [Function:periodical][].
44 1. timer - (*number*) The identifier of the setInterval (periodical) or setTimeout (delay) to clear.
48 * (*false*) returns null.
52 var myTimer = myFunction.delay(5000); //Waits 5 seconds then executes myFunction.
53 myTimer = $clear(myTimer); //Cancels myFunction.
58 - [Function:periodical][]
62 Function: $defined {#defined}
63 -----------------------------
65 Checks to see if a value is defined.
73 1. obj - (*mixed*) The object to inspect.
77 * (*boolean*) If the object passed is not null or undefined, returns true. Otherwise, returns false.
81 function myFunction(arg){
82 if($defined(arg)) alert('The object is defined.');
83 else alert('The object is null or undefined.');
88 Function: $arguments {#arguments}
89 ---------------------------------
91 Creates a function which returns the passed argument according to the index (i) passed.
95 var argument = $arguments(i);
99 1. i - (*number*) The index of the argument to return.
103 * (*function*) The function that returns a certain argument from the function's arguments.
107 var secondArgument = $arguments(1);
108 alert(secondArgument('a','b','c')); //Alerts "b".
112 Function: $empty {#empty}
113 -------------------------
115 An empty function, that's it. Typically used for as a placeholder inside event methods of classes.
119 var emptyFn = $empty;
125 Function: $lambda {#lambda}
126 -------------------------
128 Creates an empty function which does nothing but return the value passed.
132 var returnTrue = $lambda(true);
136 1. value - (*mixed*) The value for the created function to return.
140 * (*function*) A function which returns the desired value.
144 myLink.addEvent('click', $lambda(false)); //Prevents a link Element from being clickable.
147 Function: $extend {#extend}
148 ---------------------------
150 Copies all the properties from the second object passed in to the first object passed in.
154 $extend(original, extended);
158 1. original - (*object*) The object to be extended.
159 2. extension - (*object*) The object whose properties will be copied to original.
163 * (*object*) The first object passed in, extended.
176 $extend(firstObj, secondObj);
177 //firstObj is now: {'name': 'John', 'lastName': 'Dorian', 'age': '20', 'sex': 'male'};
181 Function: $merge {#merge}
182 -------------------------
184 Merges any number of objects recursively without referencing them or their sub-objects.
188 var merged = $merge(obj1, obj2[, obj3[, ...]]);
192 1. (objects) Any number of objects.
196 * (*object*) The object that is created as a result of merging all the objects passed in.
200 var obj1 = {a: 0, b: 1};
201 var obj2 = {c: 2, d: 3};
202 var obj3 = {a: 4, d: 5};
203 var merged = $merge(obj1, obj2, obj3); //returns {a: 4, b: 1, c: 2, d: 5}, (obj1, obj2, and obj3 are unaltered)
205 var nestedObj1 = {a: {b: 1, c: 1}};
206 var nestedObj2 = {a: {b: 2}};
207 var nested = $merge(nestedObj1, nestedObj2); //returns: {a: {b: 2, c: 1}}
209 Function: $each {#each}
210 -----------------------
212 Used to iterate through iterables that are not regular arrays, such as built in getElementsByTagName calls, arguments of a function, or an object.
216 $each(iterable, fn[, bind]);
220 1. iterable - (*object* or *array*) The object or array to iterate through.
221 2. fn - (*function*) The function to test for each element.
222 3. bind - (*object*, optional) The object to use as 'this' within the function. For more information see [Function:bind][].
228 fn(item, index, object)
232 1. item - (*mixed*) The current item in the array.
233 2. index - (*number*) The current item's index in the array. In the case of an object, it is passed the key of that item rather than the index.
234 3. object - (*mixed*) The actual array/object.
240 $each(['Sun','Mon','Tue'], function(day, index){
241 alert('name:' + day + ', index: ' + index);
242 }); //Alerts "name: Sun, index: 0", "name: Mon, index: 1", etc.
247 //Alerts "The first day of the week is Sunday", "The second day of the week is Monday", etc:
248 $each({first: "Sunday", second: "Monday", third: "Tuesday"}, function(value, key){
249 alert("The " + key + " day of the week is " + value);
253 Function: $pick {#pick}
254 -----------------------
256 Returns the first defined argument passed in, or null.
260 var picked = $pick(var1[, var2[, var3[, ...]]]);
264 * (*mixed*) Any number of variables.
268 * (*mixed*) The first variable that is defined.
269 * (*false*) If all variables passed in are null or undefined, returns null.
273 function say(infoMessage, errorMessage){
274 alert($pick(errorMessage, infoMessage, 'There was no message supplied.'));
276 say(); //Alerts "There was no message supplied."
277 say("This is an info message."); //Alerts "This is an info message."
278 say("This message will be ignored.", "This is the error message."); //Alerts "This is the error message."
282 Function: $random {#random}
283 ---------------------------
285 Returns a random integer between the two passed in values.
289 var random = $random(min, max);
293 1. min - (*number*) The minimum value (inclusive).
294 2. max - (*number*) The maximum value (inclusive).
298 * (*number*) A random number between min and max.
302 alert($random(5, 20)); //Alerts a random number between 5 and 20.
306 Function: $splat {#splat}
307 -------------------------
309 Converts the argument passed in to an array if it is defined and not already an array.
313 var splatted = $splat(obj);
317 1. obj - (*mixed*) Any type of variable.
321 * (*array*) If the variable passed in is an array, returns the array. Otherwise, returns an array with the only element being the variable passed in.
325 $splat('hello'); //Returns ['hello'].
326 $splat(['a', 'b', 'c']); //Returns ['a', 'b', 'c'].
330 Function: $time {#time}
331 -----------------------
333 Returns the current time as a timestamp.
341 * (*number*) - The current timestamp.
345 Function: $try {#try}
346 ---------------------
348 Tries to execute a number of functions. Returns immediately the return value of the first non-failed function without executing successive functions, or null.
352 $try(fn[, fn, fn, fn, ...]);
356 * fn - (*function*) The function to execute.
360 * (*mixed*) Standard return of the called function.
361 * (*null*) `null` if all the passed functions fail.
365 var result = $try(function(){
366 return some.made.up.object;
368 return jibberish.that.doesnt.exists;
375 var failure, success;
378 some.made.up.object = 'something';
384 if (success) alert('yey!');
386 Function: $type {#type}
387 -----------------------
389 Returns the type of object that matches the element passed in.
397 1. obj - (*object*) The object to inspect.
401 * 'element' - (*string*) If object is a DOM element node.
402 * 'textnode' - (*string*) If object is a DOM text node.
403 * 'whitespace' - (*string*) If object is a DOM whitespace node.
404 * 'arguments' - (*string*) If object is an arguments object.
405 * 'array' - (*string*) If object is an array.
406 * 'object' - (*string*) If object is an object.
407 * 'string' - (*string*) If object is a string.
408 * 'number' - (*string*) If object is a number.
409 * 'date' - (*string*) If object is a date.
410 * 'boolean' - (*string*) If object is a boolean.
411 * 'function' - (*string*) If object is a function.
412 * 'regexp' - (*string*) If object is a regular expression.
413 * 'class' - (*string*) If object is a Class (created with new Class, or the extend of another class).
414 * 'collection' - (*string*) If object is a native htmlelements collection, such as childNodes, getElementsByTagName, etc.
415 * 'window' - (*string*) If object is the window object.
416 * 'document' - (*string*) If object is the document object.
417 * false - (*boolean*) If object is undefined, null, NaN or none of the above.
421 var myString = 'hello';
422 $type(myString); //Returns "string".
426 [Array]: /Native/Array
427 [Function:bind]: /Native/Function/#Function:bind
428 [Function:delay]: /Native/Function/#Function:delay
429 [Function:periodical]: /Native/Function/#Function:periodical