Is there any difference of use, efficiency or background technique between
var mc:MovieClip = MovieClip(getChildByName("mc"));
and
var mc:MovieClip = getChildByName("mc") as MovieClip;
?
The choice is just matter of convention, preference or are there cases where you can’t use one?
This article describes the differences well:
asalso allows you to cast toArray, which wasn’t possible before since the conversion functionArray()took precedence.EDIT: concerning performance, using
asis reported to be faster than the function call style casting in various articles: [1] [2] [3]. The first article cited looks at the performance differences in depth and reports thatasis 4x-4.5x faster.EDIT 2: Not only is
as4x-4.5x faster in the normal best case, but when you wrap the(cast)style conversion in a try-catch block, and an error actually ends up being thrown, it’s more like 30x – 230x faster. In AS3, if you think you’re going to do something exceptional (in that it could throw an error) then it’s clear that you should always look before you leap. Never use try/catch unless forced to by the API, and indeed that means to never(cast)It also is instructive to look at the performance implications of try/catch even when no exception is thrown. There’s a performance penalty to setting up a try/catch block even in the happy case that nothing goes wrong.