AS3 - Returning a property of a class rather than the class itself

In ActionScript 3, there are some classes that will represent a value rather than the class itself. It's hard to explain properly what I mean, so take this example:

var str:String = "something";
var mc:MovieClip = new MovieClip();

trace(str); // something
trace(mc); // [object MovieClip]

You'll notice that the first trace outputs a value, rather than [object String]. Ontop of this, I can still make use of methods of String, like this:

var ar:Array = str.split('s');

Even though in a way you could almost read the above as:


I have a class AvLevelData that has some methods that deal with level data (which is essentially a String). At the moment there is a property data:String which represents the core level data.

The question I have is - can I replicate the behaviour of String in that when I trace or assign an instance of AvLevelData, the result is actually the String data.

For example, at the moment I need to go:

var levelData:AvLevelData = new AvLevelData();

To get the data. I instead want to be able to simply do the following:

var levelData:AvLevelData = new AvLevelData();
trace(levelData); // some level data string

Is this possible?


If you wan't your object to trace out your own fabricated string then you must implement a toString() function on your AvLevelData class.

In your example above, the MovieClip trace outputs: [Object MovieClip]; this comes from the default toString() implementation for Object (found on Object.prototype) . Note, you cannot override toString() as it only exists on the prototype of Object (remnants of the AS2/Javascript world), all you need to do is provide your own implementation with the same name. For instance:

public function toString():String {
   return "MyCustomObjectString";

Some of the most basic types - String, int, Number, uint, Boolean, to name a few - are not classes / objects per se, they are primitives. In some languages there is a wrapper class available for some of these so they can be treated like objects, though Flash doesn't do this so much from my experience.

Probably the best way to answer your question is to make a toString() method for your AvLevelData class:

public function toString():String {
    return data;

Any time you treat a class as a string (such as by putting it in trace()), flash (and many other languages) try to call toString() on the object. Typically this results in a string that's not helpful. But if you define your own toString() method, you can control what string gets output.

Another option is to simply do:


Since that variable is a string, it should trace just fine.

Need Your Help

Dynamically accessing resource file properties from Razor

c# razor

We have a partial view which renders a <ul> containing <li> items that represent menu items. These are currently static in structure, and handle multilingual with resource files as fol...

SoapClient CreateItem return null

php soap imap rhel

I got an app written on kohana fw. For some reasons I had to recompile php on linux server(rhel6) , and some processes that were working with ms exchange died.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.