Jump to content
Larry Ullman's Book Forums

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!

Link to comment
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
Link to comment
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! :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...