element.addEventListener(type,handler,useCapture);
其中handler中的this為何指向element呢甩挫?
1. IDL
DOM規(guī)范是用IDL描述的,而IDL并沒有指定某個語言椿每,例如特指EcmaScript來實現(xiàn)它伊者。
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的參數(shù)
Interface EventTarget (introduced in DOM Level 2)
指出了addEventListener的3個參數(shù):
type of type DOMString
listener of type EventListener
useCapture of type boolean
Interface EventListener (introduced in DOM Level 2)
指出listener只有一個參數(shù):
evt of type Event
參考:
Document Object Model Events
然而,到這里為止并沒有提到this的指向间护。
于是只好翻Draft了亦渗。
3. Web IDL Draft
Web IDL描述了瀏覽器環(huán)境中的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該節(jié)指出了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在這一節(jié)中有央碟,
Call listener’s callback’s handleEvent(),
with event as argument and event’s currentTarget attribute value as callback this value.
this===e.currentTarget