The prototype for an array is the original Array prototype object.
Array objects inherit many methods and properties from their prototype parent. Enquiring the prototype of a an object whose provenance is unknown will tell you what sort of object that is.
Array objects inherit these properties from the built-in Array prototype:
Array.constructor
Array.prototype
Array objects inherit these methods from the built in Array prototype:
Array.join()
Array.reverse()
Array.sort()
Array.toString()
Array instances provide these properties themselves even if the prototype has them:
Array.length
The example demonstrates how to provide extensions to all instances of this class by adding a function to the prototype object.
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT> // Define a function that duplicates the item at the end of // the array function dupe() { if(this.length != 0) { myTail = this[this.length-1]; this[this.length] = myTail; return myTail; } } // Register the new function Array.prototype.dupe = dupe; // Create an array and test the Array.dupe() method myArray = new Array("AAA", "BBB", "CCC"); document.write("Array<BR>") displayArrayAsTable(myArray); document.write("Array.dupe()<BR>") document.write(myArray.dupe()) document.write("<BR>") document.write("<BR>") document.write("Array after dupe() call<BR>") displayArrayAsTable(myArray); // Display an array in a table function displayArrayAsTable(anArray) { myLength = anArray.length; document.write("<TABLE BORDER=1>"); for(myIndex = 0; myIndex < myLength; myIndex++) { document.write("<TR><TD>"); document.write(myIndex); document.write("</TD><TD>"); document.write(anArray[myIndex]); document.write("</TD></TR>"); document.write("</TD></TR>"); } document.write("</TABLE><BR><BR>") } </SCRIPT> </BODY> </HTML>
Prev | Home | Next |
Array.pop() | Up | Array.push() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |