- Added WebKit 525 Detection (Safari 3.1 and Chrome).
[mootools/dkf.git] / Docs / Core / Core.md
blob5d8c5d6549cfd9fc5fdbcfb66676df94587fbb37
1 Core {#Core}
2 ============
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.
7 Function: $chk {#chk}
8 ---------------------
10 Checks to see if a value exists or is 0. Useful for allowing 0.
12 ### Syntax:
14         $chk(item);
16 ### Arguments:
18 1. item - (*mixed*) The item to inspect.
20 ### Returns:
22 * (*boolean*) If the object passed in exists or is 0, returns true. Otherwise, returns false.
24 ### Example:
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 ""');
29         }
33 Function: $clear {#clear}
34 -------------------------
36 Clears a Timeout or an Interval. Useful when working with [Function:delay][] and [Function:periodical][].
38 ### Syntax:
40         $clear(timer);
42 ### Arguments:
44 1. timer - (*number*) The identifier of the setInterval (periodical) or setTimeout (delay) to clear.
46 ### Returns:
48 * (*false*) returns null.
50 ### Example:
52         var myTimer = myFunction.delay(5000); //Waits 5 seconds then executes myFunction.
53         myTimer = $clear(myTimer); //Cancels myFunction.
55 ### See also:
57 - [Function:delay][]
58 - [Function:periodical][]
62 Function: $defined {#defined}
63 -----------------------------
65 Checks to see if a value is defined.
67 ### Syntax:
69         $defined(obj);
71 ### Arguments:
73 1. obj - (*mixed*) The object to inspect.
75 ### Returns:
77 * (*boolean*) If the object passed is not null or undefined, returns true. Otherwise, returns false.
79 ### Example:
81         function myFunction(arg){
82                 if($defined(arg)) alert('The object is defined.');
83                 else alert('The object is null or undefined.');
84         }
88 Function: $arguments {#arguments}
89 ---------------------------------
91 Creates a function which returns the passed argument according to the index (i) passed.
93 ### Syntax:
95         var argument = $arguments(i);
97 ### Arguments
99 1. i - (*number*) The index of the argument to return.
101 ### Returns
103 * (*function*) The function that returns a certain argument from the function's arguments.
105 ### Example:
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.
117 ### Syntax:
119         var emptyFn = $empty;
121 ### Example:
123         var myFunc = $empty;
125 Function: $lambda {#lambda}
126 -------------------------
128 Creates an empty function which does nothing but return the value passed.
130 ### Syntax:
132         var returnTrue = $lambda(true);
133         
134 ### Arguments
136 1. value - (*mixed*) The value for the created function to return.
138 ### Returns
140 * (*function*) A function which returns the desired value.
142 ### Example:
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.
152 ### Syntax:
154         $extend(original, extended);
156 ### Arguments:
158 1. original  - (*object*) The object to be extended.
159 2. extension - (*object*) The object whose properties will be copied to original.
161 ### Returns:
163 * (*object*) The first object passed in, extended.
165 ### Examples:
167         var firstObj = {
168                 'name': 'John',
169                 'lastName': 'Doe'
170         };
171         var secondObj = {
172                 'age': '20',
173                 'sex': 'male',
174                 'lastName': 'Dorian'
175         };
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.
186 ### Syntax:
188         var merged = $merge(obj1, obj2[, obj3[, ...]]);
190 ### Arguments:
192 1. (objects) Any number of objects.
194 ### Returns:
196 * (*object*) The object that is created as a result of merging all the objects passed in.
198 ### Examples:
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.
214 ### Syntax:
216         $each(iterable, fn[, bind]);
218 ### Arguments:
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][].
224 #### Argument: fn
226 ##### Syntax:
228         fn(item, index, object)
230 ##### Arguments:
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.
236 ### Examples:
238 #### Array Example:
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.
245 #### Object Example:
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);
250         });
253 Function: $pick {#pick}
254 -----------------------
256 Returns the first defined argument passed in, or null.
258 ### Syntax:
260         var picked = $pick(var1[, var2[, var3[, ...]]]);
262 ### Arguments:
264 * (*mixed*) Any number of variables.
266 ### Returns:
268 * (*mixed*) The first variable that is defined.
269 * (*false*) If all variables passed in are null or undefined, returns null.
271 ### Example:
273         function say(infoMessage, errorMessage){
274                 alert($pick(errorMessage, infoMessage, 'There was no message supplied.'));
275         }
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.
287 ### Syntax:
289         var random = $random(min, max);
291 ### Arguments:
293 1. min - (*number*) The minimum value (inclusive).
294 2. max - (*number*) The maximum value (inclusive).
296 ### Returns:
298 * (*number*) A random number between min and max.
300 ### Example:
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.
311 ### Syntax:
313         var splatted = $splat(obj);
315 ### Arguments:
317 1. obj - (*mixed*) Any type of variable.
319 ### Returns:
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.
323 ### Example:
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.
335 ### Syntax:
337         var time = $time();
339 ### Returns:
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.
350 ### Syntax:
352         $try(fn[, fn, fn, fn, ...]);
354 ### Arguments:
356 * fn   - (*function*) The function to execute.
358 ### Returns:
360 * (*mixed*) Standard return of the called function.
361 * (*null*) `null` if all the passed functions fail.
363 ### Examples:
365         var result = $try(function(){
366                 return some.made.up.object;
367         }, function(){
368                 return jibberish.that.doesnt.exists;
369         }, function(){
370                 return false;
371         });
372         
373         //result is false
374         
375         var failure, success;
376         
377         $try(function(){
378                 some.made.up.object = 'something';
379                 success = true;
380         }, function(){
381                 failure = true;
382         });
383         
384         if (success) alert('yey!');
386 Function: $type {#type}
387 -----------------------
389 Returns the type of object that matches the element passed in.
391 ### Syntax:
393         $type(obj);
395 ### Arguments:
397 1. obj - (*object*) The object to inspect.
399 ### Returns:
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.
419 ### Example:
421         var myString = 'hello';
422         $type(myString); //Returns "string".
425 [Hash]: /Native/Hash
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