Core contains an handful of common sense functions used in MooTools. It also contains some basic Hash and Array methods.
Checks to see if a value exists or is 0. Useful for allowing 0.
$chk(item);
function myFunction(arg){ if($chk(arg)) alert('The object exists or is 0.'); else alert('The object is either null, undefined, false, or ""'); }
Clears a Timeout or an Interval. Useful when working with Function:delay and Function:periodical.
$clear(timer);
var myTimer = myFunction.delay(5000); //Waits 5 seconds then executes myFunction. myTimer = $clear(myTimer); //Cancels myFunction.
Checks to see if a value is defined.
$defined(obj);
function myFunction(arg){ if($defined(arg)) alert('The object is defined.'); else alert('The object is null or undefined.'); }
Creates a function which returns the passed argument according to the index (i) passed.
var argument = $arguments(i);
var secondArgument = $arguments(1); alert(secondArgument('a','b','c')); //Alerts "b".
An empty function, that's it. Typically used for as a placeholder inside event methods of classes.
var emptyFn = $empty;
var myFunc = $empty;
Creates an empty function which does nothing but return the value passed.
var returnTrue = $lambda(true);
myLink.addEvent('click', $lambda(false)); //Prevents a link Element from being clickable.
Copies all the properties from the second object passed in to the first object passed in.
$extend(original, extension);
var firstObj = { 'name': 'John', 'lastName': 'Doe' }; var secondObj = { 'age': '20', 'sex': 'male', 'lastName': 'Dorian' }; $extend(firstObj, secondObj); //firstObj is now: {'name': 'John', 'lastName': 'Dorian', 'age': '20', 'sex': 'male'};
Merges any number of objects recursively without referencing them or their sub-objects.
var merged = $merge(obj1, obj2[, obj3[, ...]]);
var obj1 = {a: 0, b: 1}; var obj2 = {c: 2, d: 3}; var obj3 = {a: 4, d: 5}; var merged = $merge(obj1, obj2, obj3); //returns {a: 4, b: 1, c: 2, d: 5}, (obj1, obj2, and obj3 are unaltered) var nestedObj1 = {a: {b: 1, c: 1}}; var nestedObj2 = {a: {b: 2}}; var nested = $merge(nestedObj1, nestedObj2); //returns: {a: {b: 2, c: 1}}
Used to iterate through iterables that are not regular arrays, such as built in getElementsByTagName calls, arguments of a function, or an object.
$each(iterable, fn[, bind]);
fn(item, index, object)
$each(['Sun','Mon','Tue'], function(day, index){ alert('name:' + day + ', index: ' + index); }); //Alerts "name: Sun, index: 0", "name: Mon, index: 1", etc.
//Alerts "The first day of the week is Sunday", "The second day of the week is Monday", etc: $each({first: "Sunday", second: "Monday", third: "Tuesday"}, function(value, key){ alert("The " + key + " day of the week is " + value); });
Returns the first defined argument passed in, or null.
var picked = $pick(var1[, var2[, var3[, ...]]]);
null or undefined, returns null.function say(infoMessage, errorMessage){ alert($pick(errorMessage, infoMessage, 'There was no message supplied.')); } say(); //Alerts "There was no message supplied." say("This is an info message."); //Alerts "This is an info message." say("This message will be ignored.", "This is the error message."); //Alerts "This is the error message."
Returns a random integer between the two passed in values.
var random = $random(min, max);
alert($random(5, 20)); //Alerts a random number between 5 and 20.
Converts the argument passed in to an array if it is defined and not already an array.
var splatted = $splat(obj);
$splat('hello'); //Returns ['hello']. $splat(['a', 'b', 'c']); //Returns ['a', 'b', 'c'].
Returns the current time as a timestamp.
var time = $time();
Tries to execute a number of functions. Returns immediately the return value of the first non-failed function without executing successive functions, or null.
$try(fn[, fn, fn, fn, ...]);
null if all the passed functions fail.var result = $try(function(){ return some.made.up.object; }, function(){ return jibberish.that.doesnt.exists; }, function(){ return false; }); //result is false var failure, success; $try(function(){ some.made.up.object = 'something'; success = true; }, function(){ failure = true; }); if (success) alert('yey!');
Returns the type of object that matches the element passed in.
$type(obj);
var myString = 'hello'; $type(myString); //Returns "string".
This documentation is released under a Attribution-NonCommercial-ShareAlike 3.0 License.