toString Function

The toString function is a powerful help to the programmer, as it allows the programmer to define the state of an object in a concise, human readable manner. The toString function is often used in conjunction with the trace function.

The toString Function

During debugging of a program, it is often useful to see the "State" of an object. We can do this by using a "Debugger", but often there are 100s of data values associated with an object, most of which we don't care about. This is especially true of graphical objects which "extend Sprite".

The toString method is used to provide the programmer with the Most Important information in an object in a concise and Human Readable Message. Usually this involves printing the most salient object level variables in a concise manner.

For example, if you were printing a "star" you might create a message such as: "I am a star, at location 5,10, with a radius of 15" (or even: "Star: (5,10) radius:15"). The person debugging your code would be able to quickly understand the important values associated wit this star (in this case, its location and radius) and determine if these values match the programmers expectation. Finding this information with a debugger might take twice as much time (or more).


The trace function prints information to the screen for the programmer's benefit while the program is running. Usually this is "debug" information, allowing the programmer to get a quick understanding of the "state" of the program.

When you call the function on an object, the object's toString function is used.

          var x:Star = new Star(5,10,25);


          // result is based on the star's toString function, but could be:
          //   Star: (5,10) radius: 25