element.addEventListener(type,handler,useCapture);
其中handler中的this为何指向element呢?
1. IDL
DOM规范是用IDL描述的,而IDL并没有指定某个语言,例如特指EcmaScript来实现它。
As a W3C specification, one important objective for the Document Object Model is to provide a standard programming interface that can be used in a wide variety of environments and applications. The DOM is designed to be used with any programming language.
<u></u>
In order to provide a precise, language-independent specification of the DOM interfaces, we have chosen to define the specifications in OMG IDL, as defined in the CORBA 2.2 specification. In addition to the OMG IDL specification, we provide language bindings for Java and ECMAScript (an industry-standard scripting language based on JavaScript and JScript).
参考:
What is the Document Object Model?
Interface Definition Language™ (IDL™) 3.5
2. EventListener的参数
Interface EventTarget (introduced in DOM Level 2)
指出了addEventListener的3个参数:
type of type DOMString
listener of type EventListener
useCapture of type boolean
Interface EventListener (introduced in DOM Level 2)
指出listener只有一个参数:
evt of type Event
参考:
Document Object Model Events
然而,到这里为止并没有提到this的指向。
于是只好翻Draft了。
3. Web IDL Draft
Web IDL描述了浏览器环境中的IDL。
This document defines an interface definition language, Web IDL, that can be used to describe interfaces that are intended to be implemented in web browsers.
Web IDL is an IDL variant with a number of features that allow the behavior of common script objects in the web platform to be specified more readily.
4.9. User objects implementing callback interfaces该节指出了callback中this的指向
If thisArg was not given, let thisArg be undefined.
参考:
Web IDL (Second Edition) W3C Editor’s Draft 19 May 2016
4. this指向
Github上面有一个repository:WHATWG
其中,写了事件被dispatch的步骤:
3.8. Dispatching events在这一节中有,
Call listener’s callback’s handleEvent(),
with event as argument and event’s currentTarget attribute value as callback this value.
this===e.currentTarget