String.prototype (Property)

The prototype for the String object that can be used to extend the interface for all String objects.

Availability:

ECMAScript edition - 2
JavaScript - 1.1
JScript - 3.0
Internet Explorer - 4.0
Netscape - 3.0
Netscape Enterprise server - 3.0
Opera - 3.0
Property/method value type:String object
JavaScript syntax:-String.prototype
-myString.constructor.prototype

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.

Example code:

   <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>

See also:prototype property, String object, String.charAt(), String.charCodeAt(), String.constructor, String.indexOf(), String.lastIndexOf(), String.length, String.split(), String.substring(), String.toLocaleLowerCase(), String.toLocaleUpperCase(), String.toLowerCase(), String.toString(), String.toUpperCase(), String.valueOf()

Cross-references:

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