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:
Adding methods to a prototype in order to extend them to satisfy the Ecma standard is called polyfilling. In this article, we present a set of methods to add to the Array prototype to complete the Ecma 5 specification. All methods are defined within an immediately invoked function expression (IFFE) so as not to create namespace conflicts with other code:
Available since: Ecma 5.1, Chrome 5, Firefox 4.0, IE 9, Opera 10.5, Safari 5
The every method tests whether all elements in the array pass the test implemented by the provided function. The every method executes the provided callback function once for each element present in the array until it finds one where callback returns a falsy value (a value that becomes false when converted to a boolean). If such an element is found, the every method immediately returns false. Otherwise, if callback returned a true value for all elements, every will return true. The callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.
The indexOf method returns the first index at which a given element can be found in the array, or -1 if it is not present.
Elements are matched using the === operator. An index can be specified where searching should start (default start index is zero). If a negative index is specified, then the start index is counted from the back of the array where -1 corresponds to the last element in the array.
Available since: Ecma 5.1, Chrome, Firefox, IE 9, Opera, Safari
The lastIndexOf method returns the last index at which a given element can be found in the array, or -1 if it is not present.
Elements are matched using the === operator. An index can be specified where searching should start (default start index is zero). If a negative index is specified, then the start index is counted from the front of the array where -1 corresponds to the first element in the array.