MACROMEDIA FLEX-FLEX ACTIONSCRIPT LANGUAGE Reference page 385

Actionscript language reference
Table of Contents

Advertisement

Example
This example shows the return value for toString() on a generic object:
var myObject:Object = new Object();
trace(myObject.toString()); // output: [object Object]
This method can be overridden to return a more meaningful value. The following examples show
that this method has been overridden for the built-in classes Date, Array, and Number:
// Date.toString() returns the current date and time
var myDate:Date = new Date();
trace(myDate.toString()); // output: [current date and time]
// Array.toString() returns the array contents as a comma-delimited string
var myArray:Array = new Array("one", "two");
trace(myArray.toString()); // output: one,two
// Number.toString() returns the number value as a string
// Because trace() won't tell us whether the value is a string or number
// we will also use typeof() to test whether toString() works.
var myNumber:Number = 5;
trace(typeof (myNumber));
trace(myNumber.toString()); // output: 5
trace(typeof (myNumber.toString())); // output: string
The following example shows how to override
file named Vehicle.as that contains only the Vehicle class definition and place it into your Classes
folder inside your Configuration folder.
// contents of Vehicle.as
class Vehicle {
var numDoors:Number;
var color:String;
function Vehicle(param_numDoors:Number, param_color:String) {
this.numDoors = param_numDoors;
this.color = param_color;
}
function toString():String {
var doors:String = "door";
if (this.numDoors > 1) {
doors += "s";
}
return ("A vehicle that is " + this.color + " and has " + this.numDoors +
" " + doors);
}
}
// code to place into a FLA file
var myVehicle:Vehicle = new Vehicle(2, "red");
trace(myVehicle.toString());
// output: A vehicle that is red and has 2 doors
// for comparison purposes, this is a call to valueOf()
// there is no primitive value of myVehicle, so the object is returned
// giving the same output as toString().
// output: number
toString()
in a custom class. First create a text
Object.toString()
385

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Flex

Table of Contents