Actionscript 2.0 vs Object.addProperty()
Monday, 14 February 2005Over the past 6 months or so I have been utterly consumed by the task of designing and building our content management system. The administration interface is written entirely in Actionscript 2.0, and I have learned to love that language like a brother1 – I don’t think I’ll ever touch a line of Actionscript 1 again if I can help it.
In the administration application, we have a data class that represents a page that has an extensible collection of ‘properties’ (as we call them in the system – sorry about the confusing nomenclature) along with methods to allow properties to be added. The class also has a number of regular private properties, along with Actionscript 2.0 style getter / setter methods for those properties.
A super-simplified version of this class might look something like this:
class Page { private var __properties:Array; private var __title:String; private var __path:String; public function get title():String { return __title; } public function get path():String { return __path; } public function Page(Void) { __properties = new Array(); } public function addProperty(prop:Property) : Void { __properties.push(prop); } }
While the above may look fine (especially if you only code in AS2), you’ll find that the getter / setter methods don’t actually work on objects of that class.
Half a day and several strong cups of tea later, I finally remembered that the underlying Object class has a method called addProperty(), and that method is what you use to register getter / setter functions for an object in AS1. Actionscript 2.0 classes compile down to the same byte-code instructions as AS1, so your getter / setter functions actually end up as __get_PROPERTY() and __set_PROPERTY() and uses Object.addProperty() to register them against PROPERTY in your class objects.
So, because I has implemented a method called addProperty() in my own class, and because all classes are ancestors of Object, I was hiding the Object.addProperty() method, which meant that the getter /setters were never registered.
Having worked exclusively with Actionscript 2.0 for so long, it took me a good half day to figure out why this was happening. I just wanted to post this up here as a reminder so that others don’t make the same mistake! It’s certainly an easy mistake to make now that we have a proper syntax for getter/setter methods instead of having to register them ourselves. Certainly a compiler warning would have been nice!
–Update–
Typical! Having just added syntax highlighting to this blog, I can see that the addProperty() method is now highlighted as a keyword. If I’d used an editor that did this in the first place, maybe I could have saved myself from this whole sorry debacle. Sadly there’s a distinct lack of good Actionscript 2.0 editors for OS X, basically it’s TextWrangler or bust!
-
A really good brother would have given me proper private members instead of the pseudo ‘protected’ members that we have now, but that’s another post for another day. ↩





