I am currently reading the Actionscript 3 Bible and the author shows an example in which a custom eventDispatcher class is created. When the class is later used in the code it’s called as such:
//Thermometer is a custom eventDispatcher that extends the eventDispatcher class.
var thermometer:Thermometer = Thermometer(event.target); //why no new keyword?
I’m a new programmer and I was curious if someone could explain how this works? Why is the new keyword omitted? When you omit the new keyword on a class that extends another class is it essentially just calling the superclass(eventDispatcher) constructor?
This is not creating a new instance, but is casting.
What that means is that the
targetproperty of the event object is cast as Object by the Event class, and so to tell the compiler that the target is actually an instance of the Thermometer class, it is cast in this style. You could also write it like this:The two essentially mean the same thing, but when using an editor that supports code suggestion/completion for custom objects, the
Class(something)notation is preferred because it will enable the editor to provide suggestions based on your cast.