Name

prototype — NN 3 IE J2 ECMA 1

Synopsis

Read/Write

A property of the static Array object. Use the prototype property to assign new properties and methods to future instances of arrays created in the current document. For example, the following function creates a return-delimited list of elements in an array in reverse order:

function formatAsList() {
    var output = ""
    for (var i = this.length - 1; i >= 0; i--) {
        output += this[i] + "\n"
    }
    alert(output)
}

To give an array that power, assign this function reference to a prototype property whose name you want to use as the method to invoke this function:

Array.prototype.showReverseList = formatAsList

If a script creates an array at this point:

var stooges = new Array("Moe", "Larry", "Curly", "Shemp")

the new array has the showReverseList() method available to it. To invoke the method, the call is:

stooges.showReverseList()

You can add properties the same way. These allow you to attach information about the array (its creation time, for example) without disturbing the ordered sequence of array data. When a new document loads into the window or frame, the static Array object starts fresh again.

Example

Array.prototype.created = ""

Value

Any data, including function references.

Get Dynamic HTML: The Definitive Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.