I find that in my daily Flex/Flash work, I do this number a lot:
//Calling a function... MyCustomObject(container.getChildAt(i)).mySpecialFunction();
The question is – is this the best way to do this? Should I do this:
//Calling a function var tempItem:MyCustomObject = container.getChildAt(i) as MyCustomObject; tempItem.mySpecialFunction();
It might be incidental but I’m just wondering if there is an ‘accepted’ way or a preferred way to do this. The second option seems more readable but I wonder if it takes more of a performance hit to create a new variable. Or does it all come down to style and preference?
It generally doesn’t matter. Creating a var just creates a pointer to the object, so it’s not using more memory or anything like that.
The second example is definitely more readable and debuggable and should thus be preferred.
The risk you run from creating temp vars is that you might delay or prevent garbage collection for that object. This generally isn’t a problem when it’s just a local var in a function; just keep scope in mind when you’re creating vars and passing them around.
For in-depth on the subject, read Grant Skinner’s series on resource management in AVM2: http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html