Thursday, October 18, 2007

When An ActionScript Object Does Not Provide toString()

In my last blog entry, I provided some basic code that demonstrated some differences between using the ObjectUtil.toString() method to provide a textual representation of an ActionScript object's contents versus using an ActionScript object's own toString() implementation. I mentioned that ObjectUtil.toString() is an especially useful approach to see the contents of an object when that object does not have an explicit toString() method defined.

This blog entry shows the output provided by trace() from the example code in the last blog entry when the Person.toString() method has been commented out. In other words, this screenshot shows the differences between using ObjectUtil.toString() and objects' implicit toString() representation when the class has not explicitly defined toString().

As would be expected, the object-level representation is significantly less useful than the example in my last blog entry where Person's last name, first name, and age are printed out by toString(). The XMLList examples, not surprisingly, are exactly the same as in my last entry because they are based on hard-coded XML in the source code rather than on the class.

Perhaps the most interesting observation from all of this is that ObjectUtil.toString() presents the same basic information on an ActionScript object regardless of whether it has implemented a toString() method or not. This points to one of the advantages of using ObjectUtil.toString(), especially on ActionScript objects with questionable or no toString() implementations.

Side Note #1

I noted this in the last blog, but think it is worth mentioning again. When an ActionScript class explicitly defines a toString() method, that toString() method can be explicitly called by clients. However, if a toString() method is not explicitly defined for a particular ActionScript class, then toString() can only be implicitly called. This is unlike Java, where a toString() method can be called (even if its results are meaningless) on any object regardless of whether or not that Java class has specifically overridden toString().

Side Note #2

Another interesting observation brought up in my previous blog is that the methods defined in ActionScript's root Object class are overridden by first-level descendant classes by simply implementing functions with the same signature as in the Object class. This is different than ActionScript's normal use of "override" keyword to override methods of a parent class. In all cases, overridden methods must match the signature of the parent class. ActionScript does not allow for overloading of methods based on argument types or number of arguments.

No comments: