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.
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT> // 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>"); </SCRIPT> </BODY> </HTML>
See also: | toString() |
Prev | Home | Next |
prototype.constructor | Up | prototype property |
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. |