These two are wrappers for inserting and deleting elements from a particular position in an Array because I don't like the name splice
.
// insert element at index
Array.prototype.insertAt = function(element, index) {
this.splice(index, 0, element);
}
// delete element from index
Array.prototype.removeAt = function(index) {
this.splice(index, 1);
}
Some more useful Array methods to get away from using indexes:
Array.prototype.first = function() {
return this[0] || undefined;
};
Array.prototype.last = function() {
if(this.length > 0) {
return this[this.length - 1];
}
return undefined;
};
Array.prototype.max = function(array){
return Math.max.apply(Math, array);
};
Array.prototype.min = function(array){
return Math.min.apply(Math, array);
};
Some useful functions from the MooTools library:
Function.delay
Used to execute a function after the given milliseconds have elapsed.
// alerts "hello" after 2 seconds.
(function() {
alert("hello");
}).delay(2000);
Number.times
Similar to Ruby's times method for numbers, this accepts a function and executes it N times where N is the numbers value.
// logs hello 5 times
(5).times(function() {
console.log("hello");
});