Name
Object.toString( ) Method — the value of the object, as a string
Availability
Flash 5
Synopsis
someObject.toString( )
Returns
An internally defined string that describes or otherwise represents the object.
Description
The toString( )
method returns a string
description for someObject
. By default,
someObject
.toString( )
returns the expression:
"[object " + class + "]"
where class
is the internally defined name
of the class to which someObject
belongs.
The ActionScript interpreter automatically invokes the
toString( )
method whenever
someObject
is used in a string context.
For example:
x = new Object( ); trace(x); // Displays: "[object Object]"
Most classes overwrite the default toString( )
method of Object
in order to provide more
meaningful information about each member of the class. For example,
the Date.toString( )
method returns the date and
time, and the Array.toString( )
method returns a
comma-separated list of array elements. We may do the same when
constructing our own classes.
Example
This example shows how to provide a custom toString(
)
method for the Ball
class that we
created in Chapter 12:
// Add a Custom toString() method // Make the Ball constructor function Ball (radius, color, xPosition, yPosition) { this.radius = radius; this.color = color; this.xPosition = xPosition; this.yPosition = yPosition; } // Assign a function literal to the Ball class prototype's toString() method Ball.prototype.toString = function ( ) { return "A ball with the radius " + this.radius; }; // Create ...
Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.