简体   繁体   English

对象如何在JavaScript中实现Event接口

[英]How can an object implements the Event interface in JavaScript

In this .addEventListener in MDN 在此MDN中的.addEventListener中

listener 倾听者

The object that receives a notification (an object that implements the Event interface) when an event of the specified type occurs. 当指定类型的事件发生时,接收通知的对象(实现Event接口的对象)。 This must be an object implementing the EventListener interface, or simply a JavaScript function. 这必须是一个实现EventListener接口的对象,或者仅仅是一个JavaScript函数。

It says we can use an object that implements the Event interface as listener for the event. 它表示我们可以使用an object that implements the Event interface作为an object that implements the Event interface侦听器。

But I can't find how can object to implement the Event interface. 但是我找不到如何实现事件接口的对象。 As I tried: 当我尝试:

 document.querySelector('#demo').addEventListener('click', { handleEvent: function (e) { console.log(e) } }, false) 
 #demo { height: 200px; background: #ccc; } 
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div id="demo"></div> </body> </html> 

This one can handle click event right. 这个可以正确处理点击事件。

Maybe you can give me some documents about this. 也许您可以给我一些有关此的文件。

At DOM DOM

callback interface EventListener {
  void handleEvent(Event event);
};

is described at IDL Index , which links to 3.6. IDL索引中进行了描述,该索引链接到3.6。 Interface EventTarget , mentioned again at 3.8. 接口EventTarget ,在3.8再次提到 Dispatching events 调度事件

To inner invoke an object with event, run these steps: Call a user object's operation with listener's callback, " handleEvent ", a list of arguments consisting of event, and event's currentTarget attribute value as the callback this value. 内部调用带事件的对象,请执行以下步骤:使用侦听器的回调,“ handleEvent ”,由事件组成的参数列表以及事件的currentTarget属性值作为回调此值的方式调用用户对象的操作 If this throws an exception, report the exception. 如果这引发异常,请报告该异常。

An event listener can be used to observe a specific event. 事件侦听器可用于观察特定事件。

An event listener consists of these fields: 事件侦听器包含以下字段:

  • type (a string) 类型 (字符串)
  • callback (an EventListener) 回调 (一个EventListener)
  • capture (a boolean, initially false) 捕获 (布尔值,最初为false)
  • passive (a boolean, initially false) 被动的 (布尔值,最初为false)
  • once (a boolean, initially false) 一次 (布尔值,最初为false)
  • removed (a boolean for bookkeeping purposes, initially false) 移除 (用于簿记目的的布尔值,最初为false)

Although callback is an EventListener , as can be seen from the fields above, an event listener is a broader concept. 尽管回调是EventListener ,如从上面的字段中可以看到的,但事件侦听器是一个更广泛的概念。

which references back to the EventListener object where handleEvent is the only named property. 它引用回EventListener对象,其中handleEvent是唯一的命名属性。

callback interface EventListener {
  void handleEvent(Event event);
}

Web IDL clarifies Web IDL阐明

2.2. 2.2。 Interfaces 接口

The definition of EventListener as a callback interface is an example of an existing API that needs to allow user objects with a given property (in this case " handleEvent ") to be considered to implement the interface. EventListener定义为回调接口是现有API的示例,该API需要允许考虑具有给定属性的用户对象(在这种情况下为“ handleEvent ”)来实现该接口。 For new APIs, and those for which there are no compatibility concerns, using a callback function will allow only a Function object (in the ECMAScript language binding). 对于新的API,以及不存在兼容性问题的API,使用回调函数将仅允许使用Function对象(在ECMAScript语言绑定中)。

callback interface

A callback interface is an interface that uses the callback keyword at the start of its definition. 回调接口是在其定义的开头使用callback关键字的接口。 Callback interfaces are ones that can be implemented by user objects and not by platform objects, as described in §2.10 Objects implementing interfaces. 回调接口是可以由用户对象而非平台对象实现的接口,如第2.10节“实现接口的对象”中所述。

  callback interface identifier { /* interface_members... */ }; 

2.10. 2.10。 Objects implementing interfaces 对象实现接口

User objects are those that authors would create, implementing callback interfaces that the Web APIs use to be able to invoke author-defined operations or to send and receive values to the author's program through manipulating the object's attributes. 用户对象是作者将创建的对象 ,实现了Web API使用的回调接口,以便能够通过操作对象的属性来调用作者定义的操作或向作者的程序发送和接收值。 In a web page, an ECMAScript object that implements the EventListener interface, which is used to register a callback that the DOM Events implementation invokes, would be considered to be a user object. 在网页中,实现EventListener接口(用于注册DOM Events实现调用的回调)的ECMAScript对象将被视为用户对象。

Note that user objects can only implement callback interfaces and platform objects can only implement non-callback interfaces. 请注意,用户对象只能实现回调接口,而平台对象只能实现非回调接口。

For example 例如

document.querySelector('#demo').addEventListener('click', {
  abc: function (e) {
    console.log(e)
  }
}, false)

does not dispatch event to abc handler. 不会将事件调度到abc处理程序。 Though handleEvent identifier does dispatch event. 虽然handleEvent标识符确实调度事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM