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.

最新文章

  1. vuex(1.0版本写法)
  2. [转]AndroidStudio导出jar包
  3. Asp.net中Json的序列化和反序列化(二)
  4. 2016年12月13日 星期二 --出埃及记 Exodus 21:8
  5. VC++程序中加入自定义声音(PlaySound函数用法)
  6. [Lua]Lua高级教程Metatables
  7. Unity起步-1.1下载和安装Unity
  8. C语言编程练习(一)
  9. AutoCAD.net支持后台线程-Socket服务端
  10. Win32API起始处的mov edi, edi与用户空间InlineHook
  11. 图片放大功能如何做?jquery实现
  12. 【漏洞】PHPCMS_V9.6.0 前台注册GETSHELL
  13. mysql进阶(十一)外键在数据库中的作用
  14. Ubuntu16.04 将其他磁盘挂载到 /home, 解决/home空间不足
  15. Java并发编程的艺术· 笔记(1)
  16. springcloud的finchley.RC2的bug
  17. Alpha项目冲刺
  18. Sql保留两位小数方法
  19. dp之免费馅饼
  20. C#中DataTable删除多条数据

热门文章

  1. java基础2(Map)
  2. vue-cli常用配置
  3. xampp for mac配置局域网访问
  4. 异常-User class threw exception: java.lang.IllegalStateException: Cannot call methods on a stopped SparkContext.
  5. 模块之-os模块
  6. 架构师成长之路5.6-Saltstack配置管理(jinja模板)
  7. 标准C语言(3)
  8. Kay and Snowflake CodeForces - 686D (树的重心性质)
  9. STL源码阅读-traits与迭代器
  10. VMware中的桥接模式--来自网络