String.prototype : prototype « String « JavaScript Tutorial






Syntax

String.prototype.property
    String.prototype.method

The prototype property adds properties or methods to a core JavaScript object.

<html>
    <script language="JavaScript1.1">
    <!--
    function myVerify(){
      if(this.type != "Name"){
        return false;
      }else{
        return true;
      }
    }
    String.prototype.type = null;
    String.prototype.verify = myVerify;

    var myString1 = new String("This is a test");
    var myString2 = new String("This is a name");

    myString1.type = "Name";
    myString2.type = "Title";

    if(myString1.verify()){
      document.write(myString1 + " has a valid type of " + myString1.type);
    }else{
      document.write(myString1 + " has an invalid type of " + myString1.type);
    }
    document.write('<br>');

    if(myString2.verify()){
      document.write(myString2 + " has a valid type of " + myString2.type);
    }else{
      document.write(myString2 + " has an invalid type of " + myString2.type);
    }

    document.close();

    -->

    </script>
</html>








6.22.prototype
6.22.1.String.prototype