The generic behavior of this method is to return a String primitive representation of the receiving object. It is generally overridden on a class by class basis due to objects containing such different properties and values.
The ToString internal operator is called. This doesn't usually tell you very much. The default toString() handlers may be different for the built-in classes, but all you'll likely get from a class you create yourself will be the string "[object Object]".
You will need to override the toString() function that is provided by default and add your own. This should be added to the prototype of your class.
The generic version of the toString() method may be useful when debugging. You can use the apply() method to force its use on objects you are trying to inspect and which may have overridden the toString() method themselves.
This method is supported by virtually every object by virtue of the fact that it is available as a method of the Global object in Netscape. Therefore it gets inherited into the scope chain for every script and function (method).
At JavaScript version 1.2 in the Netscape version 4 browser, there is a slight difference in the way that toString() works. It will output all the nested objects that are joined by properties. This gave rise to a technique for deep copying objects. However, it wasn't ECMA compliant and it no longer works in Netscape as of JavaScript 1.3.
It might still work if you set the language version to JavaScript 1.2, but it's unreliable, not portable and definitely not going to work in Netscape 6.0. If you are exploiting it, you need to find an alternative because your scripts are going to break.
Prev | Home | Next |
ToString | Up | ToUint16 |
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. |