Difference between "Object(meuobjeto)" and "(meuobjeto as Object)"?

Asked

Viewed 65 times

2

I wish I knew the difference between using Object(meuobjeto) and (meuobjeto as Object) based on the code below:

var mc:MovieClip = new MovieClip(); //Um objeto MovieClip

trace(mc as String); //null
trace(mc as MovieClip); //[object MovieClip]
trace(mc as Number); //null

trace(String(mc)); //[object MovieClip]
trace(MovieClip(mc)); //[object MovieClip]
trace(Number(mc)); //NaN

I always use the operator as, however, only to show the Intellisense with the functions of my object and make the writing of the code a little easier. But after all, what is the difference between the two? It is right that I use it in this way?

1 answer

1


The difference between the two ways to accomplish the cast is in result when operation fails:

  • Using as

    trace(mc as MovieClip);
    

    Will assign null in cases where the conversion fails.

  • Involved in Type()

    trace(MovieClip(mc));
    

    Will generate a TypeError if the conversion fails.

Personally, the first option via as is preferable because we always have null to deal with...


Adapted to English by reply given on SOEN by the user @Marty.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.