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

2

I wanted to know 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 as operator, however, only to show intellisense with the functions of my object and make code writing a bit easier. But what is the difference between the two? Is it okay to use it that way?

    
asked by anonymous 29.10.2014 / 18:56

1 answer

1

The difference between the two ways of doing cast is in the result when the operation fails:

  • Using as

    trace(mc as MovieClip);
    

    It 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 ...

Adaptation to Portuguese of the response given in SOEN by the user @Marty.

    
30.10.2014 / 09:54