Detecting misspelled variable names in ActionScript

One of the big hassles of developing with ActionScript is that when trying to access properties and functions that do not exist, the Flash IDE does not throw an error. This can make it very difficult to debug code that is not working due to something as simple as a misspelled variable name.

Below is some simple code that shows how to trace errors when your code tries to access properties / functions that do not exist.


[code]function Foo(){}Foo.prototype.__resolve = function(name){trace(“[” + name + “] property / function does not exist”);}var f = new Foo();f.barProp;f.barFunc();[/code]

In order to use this, just add the __resolve function to the class / object / timeline that you want to watch.

It would also be very simple to write some code that loops through your code and attaches the function to all of the objects in your movie. Then when you are done developing, you just remove that code. Of course, you would have to watch out for objects that already use __resolve:

[code]if((obj[“__resolve”] == undefined)){//ok to add function}[/code]

Post any improvements or suggestions in the comments.

7 Responses to Detecting misspelled variable names in ActionScript

  1. Greg Burch says:

    This looks pretty useful. One thing to make sure it works with is inheritance. I have had some snags with __resolve causing some difficult bugs so its good to keep it as an easy to comment peice of code so that if you get wierd behavior you can make sure its not __resolve. Also another thing to check is to make sure it works well with getter/setters.

  2. Matthew Houliston says:

    Eh? Macromedians advocating undocumented features? Lol :-)Couple of swift addenda:1) this __resolve prototype will pollute for…in loops, unless hidden with ASSetPropFlags (also undocumented) or by using 2) below:2) To avoid for…in pollution, make the method more usable and also save looping to apply __resolve throughout the application, you could make the method a prototype of Object. However, this makes _global variables inaccessible outside _global’s scope. Guess there’s a reason __resolve is undocumented…I think this highlights a need for a smarter debugger in the IDE – one that can grok both logical and syntactical errors when compiling.Making user-defined functions/properties in the current script appear dynamically in the code completion menus would also rock (yes, I’ve wish-listed that one, but you guys have more influence than I 🙂

  3. Nik Khilnani says:

    so do we take it __resolve, ASSetPropFlags, ASBroadcaster etc will exist & be documented in the next version of flash?thanks,nik

  4. mike chambers says:

    You will have to wait and see…: )mike chambersmesh@macromedia.com

  5. bokel says:

    You could also use this little component to create a list of all your identifiers. This makes checking for misspelled identifiers (slightly) easier:http://www.helpqlodhelp.com/blog/archives/000026.htmlRegards,bokel

  6. jw says:

    Why not just make the SWF compiler more advanced in the next flash version…?

  7. great blog. a pleasure to be here.greetingsmanfred