开发者

Isn't it redundant to declare the data type of an event object in a listener function's parameters?

When you click on the button something happens. However it seems redundant to me that in the declaration of myListenerFunction, the event object e of class MouseEvent, actua开发者_如何转开发lly has to have its data type MouseEvent mentioned.

mybutton.addEventListener(MouseEvent.CLICK, myListenerFunction);

function myListenerFunction(e:MouseEvent):void
{
     // function body
}

Couldn't I get away with this (the .swf works just the same so far as I know...)?

 function myListenerFunction(e):void

Since the data type of e should always match the class of the event MouseEvent.CLICK (which is MouseEvent)?

EDIT:

So let's say we go from a mouse event to a keyboard event. By not declaring the data type of e, we can not be prone to errors in not changing the data type of e. e by default is going to be of type KeyboardEvent

mybutton.addEventListener(KeyboardEvent.KEY_DOWN, myListenerFunction);

function myListenerFunction(e):void
{
     // function body
}


You can keep the event type to the base class Event if you like. But you will not have access to any of the MouseEvent / KeyboardEvent-specific members when you do it like that.

Using it without a type will make it Object, which is dynamic, meaning you can try to access any member by name (even if it does not exist) - this is slower (a lot) and fairly error prone. You will not get compile time checking for example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜