prototype.toString() (Definition)

A method that you should redefine in your own classes to yield a meaningful string value.

When you create a custom object type of your own, you add various methods to it to give it some capabilities that are unique to that type (otherwise why are you creating a new object type).

Unless you override the default toString() method will be provided by the object that you cloned to make your class. By default that will be the global Object object.

The example show how to do this in a way that is consistent with normal JavaScript behavior.

Example code:

   // Make a constructor for a new class
   function Car()
      this.myClass = "Car";
   // Create a special toString() override
   function myToString()
      return "[object " + this.myClass + "]";
   // Register our overriding toString()
   Car.prototype.toString = myToString;
   // Instantiate an object from the new class
   var myCar = new Car();
   // Test the new object
   document.write("To string : " + myCar.toString() + "<BR>");

See also:toString()