Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
QuakeLive

Event Handlers Automatically Receive A Single Argument, Which Represents The Event That Occurred...

Recommended Posts

Hi,

 

 

Event handlers automatically receive a single argument, which represents the event that occurred... You can write your handlers to accept this argument:

 

          

          function someEventHandler(e) {

           // Use e.

          }

 

I do not understand how does "it" know that argument e represents event that occured, not something else. What if that is some other argument, not representing event?

 

Sorry for my bad English.

 

Thank you in advance!

Share this post


Link to post
Share on other sites

"It" (I'm assuming the callback function) knows that e represents the Event object because that's the way JavaScript is designed and implemented in all browsers.

 

Basically, whenever you have a parameter defined in a callback function header, that argument (or more specifically, the first argument) is always assigned the Event object, regardless of whether you use the variable e or some other variable name (I usually use the variable name evt).

 

If you were to call the callback function manually (i.e., not by making an event occur), then the first argument you pass the function would be assigned to e instead of the Event object. This is kind of an edge case though, as callback functions are rarely used as regular functions that are called without the occurrence of an event.

 

Does that all make sense?

  • Upvote 2

Share this post


Link to post
Share on other sites

"It" (I'm assuming the callback function) knows that e represents the Event object because that's the way JavaScript is designed and implemented in all browsers.

 

Basically, whenever you have a parameter defined in a callback function header, that argument (or more specifically, the first argument) is always assigned the Event object, regardless of whether you use the variable e or some other variable name (I usually use the variable name evt).

 

If you were to call the callback function manually (i.e., not by making an event occur), then the first argument you pass the function would be assigned to e instead of the Event object. This is kind of an edge case though, as callback functions are rarely used as regular functions that are called without the occurrence of an event.

 

Does that all make sense?

 

Yes, thank you very much HartleySan, now it is clear! :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...