Object.toString( ) Method

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
Object.toString( ) Method Flash 5

the value of the object, as a string
someObject.toString()

Returns

An internally defined string that describes or otherwise represents the object.

Description

The toString( ) method returns a string description for someObject. The ECMA-262 specification states that, by default, Object.toString( ) should return the expression:

"[object " + class + "]"

where class is the internally defined name of the class to which someObject belongs. In practice, some ActionScript classes overwrite the default toString( ) method of Object in order to provide more meaningful information about each instance of the class. For those classes that don't provide a custom toString( ) method, ActionScript does not provide the individual class name. So, the default toString( ) method for instances of most classes returns:

[object Object]

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]"

We can implement a custom toString( ) method when constructing our own classes. For example, the Date.toString( ) method returns the date and time; the Array.toString( ) method returns a comma-separated list of array elements; and the Number.toString( ) method returns a string version of the number.

Example

This example shows how to provide a custom toString( ) method for the Ball class that we created in Chapter 12:

// Make the Ball constructor function Ball (radius, color, xPosition, yPosition) {   this.radius = radius;   this.color = color;   this.xPosition = xPosition;   this.yPosition = yPosition; }     // Add a custom toString() method Ball.prototype.toString = function () {   return "A ball with the radius " + this.radius; };     // Create a new Ball object myBall = new Ball(6, 0x00FF00, 145, 200);     // Now check myBall's string value trace(myBall);  // Displays: "A ball with the radius 6"

See Also

Array.toString( ), Date.toString( ), Number.toString( ), Object.valueOf( )



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net