Availability
JavaScript 1.0; JScript 2.0; ECMAScript v1
Synopsis
object.toString( )Returns
A string representing the object.
Description
The toString( ) method is not one you often call
explicitly in your JavaScript programs. Instead, you define this
method in your objects, and the system calls it whenever it needs to
convert your object to a string.
The JavaScript system invokes the toString( )
method to convert an object to a string whenever the object is used
in a string context. For example, if an object is converted to a
string when it is passed to a function that expects a string
argument:
alert(my_object);
Similarly, objects are converted to strings when they are
concatenated to strings with the + operator:
var msg = 'My object is: ' + my_object;
The toString( ) method is invoked without
arguments and should return a string. To be useful, the string you
return should be based, in some way, on the value of the object for
which the method was invoked.
When you define a custom class in JavaScript, it is good practice to
define a toString( ) method for the class. If you
do not, the object inherits the default toString(
) method from the Object class. This default method returns
a string of the form:
[object class]
where class is the class of the object: a
value such as "Object", "String",
"Number", "Function", "Window",
"Document", and so on. This behavior of the default
toString( ) method is occasionally useful to
determine the type or class of an unknown object. Because most
objects have a custom version of toString( ),
however, you must explicitly invoke the Object.toString(
) method on an object o with code like
this:
Object.prototype.toString.apply(o);
Note that this technique for identifying unknown objects works only
for built-in objects. If you define your own object class, it will
have a class of "Object". In
this case, you can use the Object.constructor
property to obtain more information about the object.
The toString( ) method can be quite useful when
you are debugging JavaScript programs -- it allows you to print
objects and see their value. For this reason alone, it is a good idea
to define a toString( ) method for every object
class you create.
Although the toString( ) method is usually invoked
automatically by the system, there are times when you may invoke it
yourself. For example, you might want to do an explicit conversion of
an object to a string in a situation where JavaScript does not do it
automatically for you:
y = Math.sqrt(x); // Compute a number
ystr = y.toString( ); // Convert it to a string
Note in this example that numbers have a built-in toString(
) method that you can use to force a conversion.
In other circumstances, you might choose to use a toString(
) call even in a context where JavaScript would do the
conversion automatically. Using toString( )
explicitly can help to make your code clearer:
alert(my_obj.toString( ));
See Also
Object.constructor, Object.toLocaleString( ), Object.valueOf( )
•
Table of Contents
•
Index
•
Reviews
•
Examples
•
Reader Reviews
•
Errata
JavaScript: The Definitive Guide, 4th Edition
By
David Flanagan
Publisher
: O'Reilly
Pub Date
: November 2001
ISBN
: 0-596-00048-0
Pages
: 936
Slots
: 1
This fourth edition of the definitive reference to
JavaScript, a scripting language that can be embedded
directly in web pages, covers the latest version of the
language, JavaScript 1.5, as supported by Netscape 6 and
Internet Explorer 6. The book also provides complete
coverage of the W3C DOM standard (Level 1 and Level 2),
while retaining material on the legacy Level 0 DOM for
backward compatibility.