actionscript 3 - AS3: cast or "as"?

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:

A key difference between casting and the as operator is the behavior
on failure. When a cast fails in ActionScript 2, null is returned.
When a cast fails in ActionScript 3, a TypeError is thrown. With the
as operator in ActionScript 3, whenever a cast fails the default value
for the datatype is returned.

as also allows you to cast to Array, which wasn't possible before since the conversion function Array() took precedence.

EDIT: concerning performance, using as is 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 that as is 4x-4.5x faster.

EDIT 2: Not only is as 4x-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.

网友答案:

Since nobody answered the performance aspect directly yet, and it was in your question, as is dramatically more efficient and faster at runtime than (cast) in AS3.

http://jacksondunstan.com/articles/830

Combined with all the other factors I see absolutely no reason to ever use (cast) and feel it should be avoided completely.

Retracted comment below actually reminds me of a good point as well in regards to this. If you (cast) then you're almost assuredly going to find yourself in a situation where you'll have to try/catch

Prefer the use of a cast to the use of the as operator. Use the as operator only if the coercion might fail and you want the expression to evaluate to null instead of throwing an exception.

Do this:

IUIComponent(child).document

Not this:

(child as IUIComponent).document

Coding Conventions

网友答案:

(cast) and "as" are two completely different things. While 'as' simply tells the compiler to interpret an object as if it were of the given type (which only works on same or subclasses or numeric/string conversions) , the (cast) tries to use a static conversion function of the target class. Which may fail (throwing an error) or return a new instance of the target class (no longer the same object). This explains not only the speed differences but also the behavior on the Error event, as described by Alejandro P.S.

The implications are clear:
'as' is to be used if the class of an object is known to the coder but not to the compiler (because obfuscated by an interface that only names a superclass or '*'). An 'is' check before or a null check after (faster) is recommended if the assumed type (or a type compatible to auto-coercion) cannot be 100% assured.

(cast) is to be used if there has to be an actual conversion of an object into another class (if possible at all).

网友答案:

var mc:MovieClip = MovieClip(getChildByName("mc"));

will DIRECTLY SET IT AS movieclip

var mc:MovieClip = getChildByName("mc") as MovieClip;

will make mc act like a movieclip, if required type are same

网友答案:

Further to launch or not RTE, or return null, there is a significant difference when we manage errors in a swf loaded into a separate application domain.

Using Loader.uncaughtErrorEvents to handle errors of the loaded swf; if we cast like 'event.error as Error', the resulting error will have the original stack trace (the same that had been caught in the swf that caused the error) while if cast that with Error (event.error), the stack trace of the error will be changed by the current stack trace (in which the cast was made).