[9] [[DOMイベント]]における[DFN[[RUBYB[[[対象]]]@en[target]]]]とは、
[[イベント]]が[[ディスパッチ]]されたり、[[イベントリスナー]]が呼び出されたりする際の文脈として用いられる[[オブジェクト]]です。

* 仕様書

[REFS[
- [10] [CITE@en-US[DOM Standard]] ([TIME[2014-05-01 09:45:30 +09:00]] 版) <http://dom.spec.whatwg.org/#dom-event-target>
- [3] [CITE@en-US[DOM Standard]] ([TIME[2013-04-26 20:00:45 +09:00]] 版) <http://dom.spec.whatwg.org/#interface-eventtarget>
- [25] [CITE@en-US[DOM Standard]] ([TIME[2014-05-01 09:45:30 +09:00]] 版) <http://dom.spec.whatwg.org/#concept-event-dispatch>
]REFS]

* ディスパッチの対象

[22] [[イベント]]の[[対象]]は、仕様上は、
> [[disptch]] at [VAR@en[target]]
> [[fire]] at [VAR@en[target]]

... のような表現で記述されます。

[23] [CODE(DOMm)@en[[[dispatchEvent]]]] [[メソッド]]では、[[文脈オブジェクト]]が[[対象]]となります。

[24] 仕様書上の[[ディスパッチ]]には、[RUBYB[[[対象上書き]]]@en[target override]]という[[引数]]があります [SRC[>>25]]。
これが指定されていると、通常とは異なる[[オブジェクト]]が[[対象]]になります。
現在これが用いられているのは、
[FIG(list)[
- [29] [[stops parsing]] や [CODE(JS)@en[[[window.open]]]]
で発生する [CODE(DOMi)@en[[[Window]]]] [[オブジェクト]]での [CODE(DOMe)@en[[[load]]]]
[[イベント]] ([CODE(DOMi)@en[[[Document]]]] が[[対象]])
- [30] [[stops parsing]] や [[traverse the history]] で発生する [CODE(DOMi)@en[[[Window]]]] [[オブジェクト]]での [CODE(DOMe)@en[[[pageshow]]]]
[[イベント]] ([CODE(DOMi)@en[[[Document]]]] が[[対象]])
- [31] [[unload a document]] 中で発生する [CODE(DOMi)@en[[[Window]]]] [[オブジェクト]]での [CODE(DOMe)@en[[[pagehide]]]]
[[イベント]] ([CODE(DOMi)@en[[[Document]]]] が[[対象]])
- [32] [[unload a document]] 中で発生する [CODE(DOMi)@en[[[Window]]]] [[オブジェクト]]での [CODE(DOMe)@en[[[unload]]]]
[[イベント]] ([CODE(DOMi)@en[[[Document]]]] が[[対象]])
]FIG]
... です。

;; [35] これらは [CODE(DOMi)@en[Document]] ではなく [CODE(DOMi)@en[Window]]
で[[発火]]されるわけですから、 [CODE(DOMi)@en[Document]] の[[イベントリスナー]]は呼び出されず、
[CODE(DOMi)@en[Window]] の[[イベントリスナー]]だけが呼び出されます。
更には、 本来なら [CODE(JS)@en[event.target]] も [CODE(JS)@en[event.currentTarget]]
も [CODE(DOMi)@en[Window]] になるはずなのですが、
[CODE(JS)@en[event.target]] はなぜか上書きされて [CODE(DOMi)@en[Document]]
になるのです。

;; [41] [[dispatch]] も参照。

* [CODE(DOMi)@en[Event]] インターフェイス [CODE(DOMi)@en[target]] 属性 (DOM)

[13] [CODE(DOMi)@en[[[Event]]]] [[インターフェイス]]の [DFN[[CODE(DOMa)@en[[[target]]]]]]
[[属性]]は、[[イベント]]の[[対象]]を返します。

[14] [CODE[Event]] [[インターフェイス]]の
[CODE[target][event target]] [[IDL属性]]と
[DFN[[CODE[srcElement]]]] [[IDL属性]]の[[取得器]]は、
次のようにしなければ[MUST[なりません]] [SRC[>>3]]。

[FIG(steps)[
= [65] [[文脈オブジェクト]]の[F[対象][イベント対象]]を返します。
]FIG]

[63] [[イベント][イベントオブジェクト]]は、
[DFN[[F[[RUBYB[[[対象][イベント対象]]]@en[target]]]]]]を持ちます
[SRC[>>3]]。初期値は [CODE[null]] です [SRC[>>3]]。

[64] [[ディスパッチ]]の際に値が[[ディスパッチ]]対象の
[CODE(DOMi)@en[[[EventTarget]]]] に設定されます。

* [CODE(DOMi)@en[Event]] インターフェイス [CODE(DOMi)@en[currentTarget]] 属性 (DOM)

[15] [CODE(DOMi)@en[[[Event]]]] [[インターフェイス]]の [DFN[[CODE(DOMa)@en[[[currentTarget]]]]]]
[[属性]]は、[[イベント]]の現在の[[対象]]を返します。

[16] この[[属性]]の初期値は [[null]] です [SRC[>>3]]。[[イベントリスナー]]が呼び出される際に登録元の
[CODE(DOMi)@en[[[EventTarget]]]] に設定されます。

[17] [[親]]がない [CODE(DOMi)@en[[[EventTarget]]]] なら普通 [CODE(DOMa)@en[[[target]]]] と
[CODE(DOMa)@en[[[currentTarget]]]] は一致しますが、[[DOM木]]中の[[節点]]など[[親子]]関係があると、
[[捕獲]]や [[bubble]] が発生するので、等しくないことがあります。

* [CODE(DOMi)@en[EventTarget]] インターフェイス (DOM)

[11] [[DOMイベント]]の[[対象]]となり得る[[オブジェクト]]は、すべて [DFN[[CODE(DOMi)@en[[[EventTarget]]]]]]
[[インターフェイス]]を[[継承]]しています。

;; [12] [[イベント鎖]]上の[[節点]]のすべてが [CODE(DOMi)@en[[[EventTarget]]]] であることは
[[DOM Standard]] 上明示的には要求されていないようですが、 [CODE(DOMi)@en[[[Event]]]]
[[インターフェイス]]の [CODE(DOMa)@en[[[currentTarget]]]] が [CODE(DOMi)@en[[[EventTarget]]]]
型となっているので、そうでなければ要件を満たせません。

** コンストラクター

[44] [CODE(DOMi)@en[EventTarget]] [[コンストラクター]]は、次のようにしなければ[MUST[なりません]]
[SRC[>>3]]。

[FIG(steps)[
= [45] 新しい [CODE(DOMi)@en[EventTarget]] を返します。
]FIG]

** メンバー

[1] 次の[[メソッド]]が定義されています [SRC[>>10]]。
[FIG(short list)[
- [CODE(DOMm)@en[[[addEventListener]]]]
- [CODE(DOMm)@en[[[dispatchEvent]]]]
- [CODE(DOMm)@en[[[removeEventListener]]]]
]FIG]

[7] 次の状態を持ちます。

[FIG(list members)[
: [F[イベントリスナーリスト]] : 0個以上の[[イベントリスナー]]の[[リスト]]です。
[[イベント]]の[[dispatch]]の際に使われます。
: [F[[[親の取得]]]] : [[dispatch]] における[[親]]を取得する[[手順]]です。
[CODE(DOMi)@en[[[EventTarget]]]] [[オブジェクト]]の種別ごとに定義されています。
: [F[活性化動作]] :
: [F[遺物活性化前動作]] :
: [F[遺物被取消活性化動作]] :
]FIG]

** イベントリスナーリスト

[8] [[著者]]は、 [CODE(DOMi)@en[[[EventTarget]]]] の[[メソッド]]を通して[F[イベントリスナーリスト]]に含まれる個々の[[イベントリスナー]]を追加したり削除したりできます。

[33] [[イベントリスナーリスト]]に対する[[演算]]は、[[イベントリスナー]]を参照。

* [CODE(DOMi)@en[EventTarget]] の一覧

[27] 次の[[インターフェイス]]は [CODE(DOMi)@en[[[EventTarget]]]] を[[継承]]しています
(直接[[継承]]しているもののみ)。
[FIG(list middle)[
- [CODE(DOMi)@en[[[Node]]]] [SRC[[[DOM Standard]]]]
- [CODE(DOMi)@en[[[Window]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[WorkerGlobalScope]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[TextTrack]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[TextTrackCue]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[AudioTrackList]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[VideoTrackList]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[TextTrackList]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[Notification]]]] [SRC[[[Notifications API Standard]]]]
- [CODE(DOMi)@en[[[ApplicationCache]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[EventSource]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[WebSocket]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[MessagePort]]
- [CODE(DOMi)@en[BroadcastChannel]]
- [CODE(DOMi)@en[[[Worker]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[SharedWorker]]]] [SRC[[[HTML Standard]]]]
- [CODE(DOMi)@en[[[XMLHttpRequestEventTarget]]]] ([CODE(DOMi)@en[[[XMLHttpRequest]]]], [CODE(DOMi)@en[[[XMLHttpRequestUpload]]]]) [SRC[[[XHR]]]]
- [CODE(DOMi)@en[[[RTCPeerConnection]]]] [SRC[[[WebRTC]]]]
- [CODE(DOMi)@en[[[RTCDataChannel]]]] [SRC[[[WebRTC]]]]
- [CODE(DOMi)@en[[[IDBRequest]]]] [SRC[[[IndexedDB]]]]
- [CODE(DOMi)@en[[[IDBDatabase]]]] [SRC[[[IndexedDB]]]]
- [CODE(DOMi)@en[[[IDBTransaction]]]] [SRC[[[IndexedDB]]]]
- [CODE(DOMi)@en[Performance]]
- [CODE(DOMi)@en[MediaSource]]
- [CODE(DOMi)@en[SourceBuffer]]
- [CODE(DOMi)@en[SourceBufferList]]
- [CODE(DOMi)@en[ServiceWorkerContainer]]

[HISTORY[
- [CODE(DOMi)@en[[[MediaController]]]] [SRC[[[HTML Standard]]]]
]HISTORY]
]FIG]

[46] 他に、 [CODE(DOMi)@en[EventTarget]] [[コンストラクター]]で作成された素の
[CODE(DOMi)@en[EventTarget]] [[オブジェクト]]が[TIME[2017年][year:2017]]の
[CITE[DOM Standard]] の改訂で追加されました。[[著者]]が[[サブクラス化]]して任意の
[[JavaScript]] [[オブジェクト]]に使うことが想定されています。

* エラーの報告における対象

[18] [[エラーの報告]]においては、 [CODE(DOMe)@en[[[error]]]] [[イベント]]が[[ディスパッチ]]される[[オブジェクト]]が[[対象]]と呼ばれています。

[19] [[エラーの報告]]の[[対象]]となるのは、[[大域オブジェクト]]のみです。

[20] [[エラーの報告]]の[[対象]]には、[[エラー報告モード]]フラグがあります。

;; [21] 詳細は、[[エラーの報告]]の項を参照してください。

* 歴史

** 実装関係から継承関係へ

[4] [[DOM2]] や [[DOM3]] の頃は [CODE(DOMi)@en[[[EventTarget]]]] は他の[[インターフェイス]]と[[継承]]関係になく、
[[言語束縛]]依存の[[キャスト]]によって取得できるとされていました。 [[JavaScript]]
の場合は関係する[[オブジェクト]]に [CODE(DOMi)@en[[[EventTarget]]]]
のメソッドも生えている形になっていました。 (実装上は [CODE(DOMi)@en[[[Node]]]]
などの親クラスとなっていたようです。)

[5] [[WebIDL]] の時代になって [CODE[[[implements]]]] によって [[IDL]]
中にこれを記述できるようになりました。

[6] 更に [[DOM Standard]] により [CODE(DOMi)@en[[[Node]]]] などが[[継承]]する親インターフェイスとして記述されるようになりました
[SRC[>>2]]。

[REFS[
- [2] [CITE@en[Web Applications 1.0 r6378     Move the Web to a world where things that implement EventTarget just inherit from it. I, for one, now welcome our new EventTarget overlords.]]
( ([TIME[2011-08-06 08:43:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=6377&to=6378>
]REFS]

** 対象上書きの標準化

[REFS[
- [39] [CITE@en['''['''''']''' (0) Simplify event terminology. Make 'load' events fired on the Wi…]] ([[Hixie]]著, [TIME[2009-04-27 04:42:04 +09:00]]) <https://github.com/whatwg/html/commit/86fa645253b1ec11842545b33e51c31f5acfc0c6>
- [36] [CITE@en['''['''giow''']''' (2) Make pageshow/pagehide use PageTransitionEvent, to match r…]] ([[Hixie]]著, [TIME[2010-02-23 20:29:14 +09:00]]) <https://github.com/whatwg/html/commit/9d17f6130b0dbcd46be35d952604ae7267b653e6>
- [26] [CITE@en[Web Applications 1.0 r8609     align 'load' and 'unload' events since they have similar quirks in practice]] ([TIME[2014-05-07 07:16:00 +09:00]] 版) <http://html5.org/tools/web-apps-tracker?from=8608&to=8609>
- [28] [CITE@en[Web Applications 1.0 r8710 Use the 'target override' where appropriate]]
( ([TIME[2014-08-06 04:57:00 +09:00]] 版))
<http://html5.org/r/8710>
]REFS]
**

[37] [CITE[on() / off() sketch.]]
([TIME[2015-07-10 00:13:26 +09:00]] 版)
<https://gist.github.com/annevk/5238964>

[38] [CITE@en[Fix the incorrect side effects of dispatch' target override · whatwg/dom@b8a8eb2]]
([TIME[2015-11-23 17:49:54 +09:00]] 版)
<https://github.com/whatwg/dom/commit/b8a8eb272d1ff3c654b3c5ae690c990f1d0cd700>

[40] [CITE@en[Redesign fire an event and target override]]
([[annevk]]著, [TIME[2016-10-15 00:32:41 +09:00]])
<https://github.com/whatwg/dom/commit/e19d7eef74fb83792415a4a78ed866d28f4cc543>

[42] [CITE@en[Editorial: major cleanup around firing and dispatching events]]
([[annevk]]著, [TIME[2016-10-19 01:00:43 +09:00]])
<https://github.com/whatwg/html/commit/8ffbd146d4709057e45dd6cfc81e7a70897050c5>

[43] [CITE@en[Allow constructing and subclassing EventTarget]]
([[domenic]]著, [TIME[2017-07-08 03:55:17 +09:00]])
<https://github.com/whatwg/dom/commit/c4c1c8b47340a1e5ecc1a07670927b831f240586>

[47] [CITE@en[16487 – EventTarget constructor]]
([TIME[2017-07-25 16:51:30 +09:00]])
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=16487>

[48] [CITE@en[Make EventTarget subclassable · Issue #441 · whatwg/dom]]
([TIME[2017-07-25 16:55:11 +09:00]])
<https://github.com/whatwg/dom/issues/441>

[49] [CITE@en[Allow constructing and subclassing EventTarget by domenic · Pull Request #467 · whatwg/dom]]
([TIME[2017-07-25 16:56:21 +09:00]])
<https://github.com/whatwg/dom/pull/467>

[50] [CITE@en[Expose Event and EventTarget to AudioWorklet]]
([[hoch]]著, [TIME[2017-09-30 18:22:32 +09:00]])
<https://github.com/whatwg/dom/commit/601655d48abe3979200e42cefad60cdf0d752693>

[51] [CITE@en['''['''audioworklet''']''' Ensure MessagePort is exposed in audio worklets · Issue #1266 · WebAudio/web-audio-api]]
([TIME[2017-10-01 16:24:59 +09:00]])
<https://github.com/WebAudio/web-audio-api/issues/1266>

[52] [CITE@en[Expose Event, EventTarget to AudioWorklet · Issue #515 · whatwg/dom]]
([TIME[2017-10-01 16:26:05 +09:00]])
<https://github.com/whatwg/dom/issues/515>

[53] [CITE@en[Fix 515: Expose Event, EventTarget to AudioWorklet by hoch · Pull Request #516 · whatwg/dom]]
([TIME[2017-10-01 16:26:26 +09:00]])
<https://github.com/whatwg/dom/pull/516>

[54] [CITE@en[Clear event's target and relatedTarget when dispatch ends at a shadow…]]
([[TakayoshiKochi]]著, [TIME[2017-10-05 17:18:37 +09:00]])
<https://github.com/whatwg/dom/commit/9ada239e14fe7964577276ef9d8972069e665247>

[55] [CITE@en[Event.target should return null after dispatch is complete, when stopped at ShadowRoot · Issue #511 · whatwg/dom]]
([TIME[2017-10-10 22:27:33 +09:00]])
<https://github.com/whatwg/dom/issues/511>

[56] [CITE@en[Set event.target to null when dispatching finishes at ShadowRoot by TakayoshiKochi · Pull Request #513 · whatwg/dom]]
([TIME[2017-10-10 22:30:44 +09:00]])
<https://github.com/whatwg/dom/pull/513>

[57] [CITE@en[Explain why OffscreenCanvas is an EventTarget]]
([[GloverDonovan]]著, [TIME[2018-02-22 18:08:42 +09:00]])
<https://github.com/whatwg/html/commit/eb2d54687d0e9fa9e9f70d107fc07663c17c73af>

[58] [CITE@en[Explain why OffscreenCanvas inherits EventTarget by GloverDonovan · Pull Request #3474 · whatwg/html]]
([TIME[2018-02-23 13:15:08 +09:00]])
<https://github.com/whatwg/html/pull/3474>

[59] [CITE@en[Editorial: add "add an event listener" hook]]
([[annevk]]著, [TIME[2018-03-14 17:40:36 +09:00]])
<https://github.com/whatwg/dom/commit/2bdabb15de8112d9783cf393a3d6c37911149f77>

[61] [CITE@en[Editorial: add "add an event listener" hook by annevk · Pull Request #596 · whatwg/dom]]
([TIME[2018-03-18 00:16:02 +09:00]])
<https://github.com/whatwg/dom/pull/596>

[62] [CITE@en[Editorial: create a concept for event's target]]
([[annevk]]著, [TIME[2018-03-30 13:24:45 +09:00]])
<https://github.com/whatwg/dom/commit/07c110c87b8e4b1f7ecae09727b859bb75b17a73>

[66] [CITE@en[Define a target concept that Event.target and Event.srcElement reflect · Issue #570 · whatwg/dom]]
([TIME[2018-03-31 23:13:05 +09:00]])
<https://github.com/whatwg/dom/issues/570>

[67] [CITE@en[Editorial: create a concept for event's target by annevk · Pull Request #621 · whatwg/dom]]
([TIME[2018-03-31 23:13:33 +09:00]])
<https://github.com/whatwg/dom/pull/621>

[68] [CITE@en[Define Event's srcElement and returnValue]]
([[miketaylr]]著, [TIME[2018-03-31 05:49:42 +09:00]])
<https://github.com/whatwg/dom/commit/e36b369a418663603e081d211dc844d197d97ecb>

[34] [CITE@en[Align with IDL constructor changes]]
([[autokagami]]著, [TIME[2019-09-24 22:42:56 +09:00]])
<https://github.com/whatwg/dom/commit/57512fac17cf2f1c4c85be4aec178c8086ee5ee4>