码迷,mamicode.com
首页 > 编程语言 > 详细

What exactly is the parameter e (event) and why pass it to JavaScript functions?

时间:2019-07-11 14:29:39      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:with   scree   java   list   pos   cep   play   tin   out   

What exactly is the parameter e (event) and why pass it to JavaScript functions?

问题

Well, when I learned JavaScript, all the books and Internet articles I read showed code passing a parameter e to functions that handle JavaScript events, such as the code block below:

function myEvent(e) {
    var evtType = e.type
    alert(evtType)
    // displays click, or whatever the event type was
}

I‘ve always accepted that as being the way it is, but now I have some questions (this is very confusing to me):

  1. Where does this e come from? When I look at the entire JavaScript file, e does not seem to exist at all.
  2. Why pass this parameter e to functions? Will functions stop working if I do not pass e to them?
  3. Consider the code block below. There is an event variable (e) passed to an anonymous inner function. Let‘s say I want to use an event object outside of the anonymous function (maybe in a line above/below the element.onkeypress line). How can I do this?

    element.onkeypress = function(e) {
        if(e.keyCode) {
            element.keyCode = e.keyCode;
        } else {
            element.keyCode = e.charCode;
        }
    };

解答

The e is short for event

The simplest way to create an event is to click somewhere on the page.

When you click, a click event is triggered. This event is actually an object containing information about the action that just happened. In this example‘s case, the event would have info such as the coordinates of the click (event.screenX for example), the element on which you clicked (event.target), and much more.

Now, events happen all the time, however you are not interested in all the events that happen. When you are interested in some event however, it‘s when you add an event listener to the element you know will create events[1]. For example you are interested in knowing when the user clicks on a ‘Subscribe‘ button and you want to do something when this event happens.

In order to do something about this event you bind an event handler to the button you are interested in. The way to bind the handler to the element is by doing element.addEventListener(eventName, handler).

eventName is a string and it‘s the name of the event you are interested in, in this case that would be ‘click‘ (for the click event).

The handler is simply a function which does something (it‘s executed) when the event happens. The handler function, by default, when executed is passed the event object (that was created when the event/action you are interested in happened) as an argument.

Defining the event as a parameter of your handler function is optional but, sometimes (most times), it is useful for the handler function to know about the event that happened. When you do define it this is the e you see in the functions like the ones you mentioned. Remember, the event is just a regular javascript object, with lots of properties on it.

Hope that helped.

For more info read Creating and Triggering Events

As for your 3rd question, now you should know you cannot do that, because e only exists when an event happens. You could have the handler function, which has access to the e object when it gets executed, to store it in some global variable and work on that.

[1] That is not exactly correct, but it‘s simpler to understand. The more correct thing to say there is "add an event listener to the element you know will have events flow through it". See this for more information.

 

What exactly is the parameter e (event) and why pass it to JavaScript functions?

标签:with   scree   java   list   pos   cep   play   tin   out   

原文地址:https://www.cnblogs.com/chucklu/p/11169510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!