
Migrating the Event object 113
Migrating the Event object
This section describes migration issues related to accessing the Event object.
Using the Event object
The Event object is no longer of type Object. It is now of type flash.events.Event. Specify a
stricter type in functions, as follows:
private function eventHandler(event:Object):Void { // Flex 1.5
private function eventHandler(event:Event):void { // Flex 2
You should also now cast the event.target in an event handler to an appropriate type to
avoid compile-time warnings. The new code should specify the stricter event type as a
function parameter, as the following example shows:
private function doDragExit(event:DragEvent):void {
event.target.hideDropFeedback(event);
}
If you assign the target to a stronger-typed variable, you must cast; for example:
var clickTarget:Button = Button(event.target);
When using the Event object, you are encouraged to use the most strict type possible. This
lets you access properties that are specific to the target type. For example, in a mouse click
listener, you should declare the Event of type MouseEvent rather than of type Event, as
follows:
private function myClickHandler(event:MouseEvent):void { ... }
If you use Event as the type, rather than MouseEvent, you do not have access to any properties
that are specific to the MouseEvent class.
Using the target property
Calling methods and accessing properties on the target can be confusing. The default type of
Event.target is Object. Because ActionScript is strongly typed, you can call
event.target.methodName() only if you cast the event.target to an object type that
defines that
methodName. The same applies for properties. You can access
event.target.property only if you define property on the new type.
Comentarios a estos manuales