Remember type casting from as2?
Well, while they are not just yet things of the past, they have greatly been improved upon. as treats a current object in the noun sense of the word as in the same sense of the word you wish it to be. At some point in flash’s events that fire, calling currentTarget or target, simplify the object that passed through them. Because the flash player doesn’t know about your custom-type classes, it just recognizes your class when passed through events as regular objects. This is just one case in which as comes to the rescue. rather than typeCast parenthetically, which can go wrong depending on what you cast it into, use as followed by the Type you wish to treat the obj as.
although the evnt.target points to a custom class, its no longer anything other than recognized as an object.
to prove that its an object we could say typeof(evnt.target) which will trace out object.
but lets say we need to now use evnt.target as an argument in a function that only accepts CustomClass as a type,
the compiler will throw an error, because evnt.target is not a CustomClass anylonger.
But we know this is a type CustomClass, cause we declared it above in instantiation. I could prove that it is with the use of the key word is. Kind of like instanceof which is deprecated in AS3 you can compare the object to a customclass and see if it returns the class or a null which means its not recognized as that class.
trace(customClass is CustomClass) // [CustomClass Object]
yay it is part of our custom class after all
trace(customClass is Object) //[object object] and its an object as well
What is actually doing is looking at the classes string of prototypes and checking to see if it has either inherited from the class in question or implemented it as an interface.
Still flash wont allow us to pass customClass into our function which only accepts CustomClass, types.
This is where as is a beautiful art. create a new var if you wish and set it to equal the event.target as CustomClass
var iknowCustom:CustomClass= event.target as CustomClass
Because event.target has CustomClass attached to its prototype, it allows it to be cast and now we can send iknowCustom as our argument param to the function.
Viola as is.
Also very useful when using getDefinitionbyClass()