The prototype property refers to the built-in String prototype object.
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 extends the capabilities of String objects function reverse() { myArray = new Array(this.length); for(myEnum=0; myEnum<this.length; myEnum++) { myArray[myEnum] = this.substr(myEnum,1) } myArray.reverse(); return myArray.join(""); } // Register the new function String.prototype.reverse = reverse; // Create a string object and test the String.reverse() method myString = new String("ABCDEFGH"); document.write(myString.reverse()) document.write("<BR>") </SCRIPT> </BODY> </HTML>
ECMA 262 edition 2 - section 15.2.3.1
ECMA 262 edition 2 - section 15.5.3.1
ECMA 262 edition 2 - section 15.5.4
ECMA 262 edition 3 - section 15.5.3.1
Prev | Home | Next |
String.match() | Up | String.replace() |
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. |