After this code is executed, any other code can now call the hello method on any array. There is no need to instantiate a special array class, or to make any modifications to the code. This is the magic of prototypes:
In this article, we present a set of methods to add to the Array prototype that implement part of the Ruby array methods. All methods are defined within an immediately invoked function expression (IFFE) so as not to create namespace conflicts with other code:
Returns the element at index. A negative index counts from the end of self, where -1 is the last element.
Removes all elements from self and returns self for chaining purposes.
Returns a copy of self with all undefined elements removed, turning a sparse array into a dense array. Returns self for chaining purposes.
Removes null and undefined elements from the array, turning it into a dense array. Returns self for chaining purposes.
Deletes the element at the specified index, returning that element, or undefined if the index is out of range. A negative index is counted from the end of the array, where -1 corresponds to the last element. Returns self for chaining purposes.
Deletes every element of self for which function evaluates to true. Sparse arrays are converted to dense arrays. The array is changed instantly every time the function is called, not after the iteration is over.
Drops first n elements from array and returns the rest of the elements in a new array.
Drops elements up to, but not including, the first element for which the function returns false and returns an array containing the remaining elements.
Tries to return the element at position index, but throws a RangeError exception if the referenced index lies outside of the array bounds. This error can be prevented by supplying a second argument, which will act as a default value. Alternatively, if a function is given it will only be executed when an invalid index is referenced. Negative values of index count from the end of the array.
The first two forms set the selected elements of self (which may be the entire array) to obj.
A start of null / undefined is equivalent to zero. A length of null / undefined is equivalent to the length of the array.
The last two forms fill the array with the value of the given function, which is passed the absolute index of each element to be filled.
Negative values of start count from the end of the array, where -1 is the last element.
Returns the index of the first object for which the function returns true . Returns undefined if no match is found.
Returns the first element, or the first n elements, of the array. If the array is empty, requesting one element returns undefined, and requesting multiple elements returns an empty array.
Returns a new array that is a one-dimensional flattening of self (recursively). That is, for every element that is an array, extract its elements into the new array. The optional level argument determines the level of recursion to flatten.
Returns true if the given object is present in self (that is, if any element === object), otherwise returns false.
Inserts the given values before the element with the given index.
Negative indices count backwards from the end of the array, where -1 is the last element. If a negative index is used, the given values will be inserted after that element, so using an index of -1 will insert the values at the end of the array.
Returns true if self contains no elements.
Deletes every element of self for which the given function evaluates to false.
Returns the last element(s) of self. If the array is empty, returns undefined if only one element requested.
Returns a new array containing the items in self for which the given block is not true.
Same as reject, works on self.
Reverses self in place.
Returns the index of the last object for which the function returns true. Returns undefined if no match is found.
Returns a new array with elements of self shuffled.
Returns first n elements from the array.
Passes elements to the function until the function returns false, then stops iterating and returns an array of all prior elements.
Returns a new array by removing duplicate values in self. Comparison is done by default using the === operator.
If a function is given, it will use the return value of the function for comparison.