EventTarget

EventTarget은 이벤트를 받을 수 있으며, 이벤트에 대한 수신기(listener)를 가질 수 있는 객체가 구현하는 DOM 인터페이스입니다.

Element, documentwindow가 가장 흔한 이벤트 대상이지만, 다른 객체, 예컨대 XMLHttpRequest, AudioNode, AudioContext 등 역시 이벤트 대상이 될 수 있습니다.

많은 이벤트 대상(요소, 문서, 창, ...)은 onevent 속성과 특성을 사용한 이벤트 처리기 등록도 지원합니다.

생성자

EventTarget()
새로운 EventTarget 객체 인스턴스를 생성합니다.

메서드

EventTarget.addEventListener()
EventTarget에 특정 이벤트 유형의 이벤트 처리기를 등록합니다.
EventTarget.removeEventListener()
EventTarget에서 주어진 이벤트 수신기를 제거합니다.
EventTarget.dispatchEvent()
EventTarget에 이벤트를 디스패치 합니다.

예제

간단한 EventTarget 구현

var EventTarget = function() {
  this.listeners = {};
};

EventTarget.prototype.listeners = null;
EventTarget.prototype.addEventListener = function(type, callback) {
  if (!(type in this.listeners)) {
    this.listeners[type] = [];
  }
  this.listeners[type].push(callback);
};

EventTarget.prototype.removeEventListener = function(type, callback) {
  if (!(type in this.listeners)) {
    return;
  }
  var stack = this.listeners[type];
  for (var i = 0, l = stack.length; i < l; i++) {
    if (stack[i] === callback){
      stack.splice(i, 1);
      return;
    }
  }
};

EventTarget.prototype.dispatchEvent = function(event) {
  if (!(event.type in this.listeners)) {
    return true;
  }
  var stack = this.listeners[event.type].slice();

  for (var i = 0, l = stack.length; i < l; i++) {
    stack[i].call(this, event);
  }
  return !event.defaultPrevented;
};

명세

명세 상태 설명
DOM
The definition of 'EventTarget' in that specification.
Living Standard 변화 없음.
Document Object Model (DOM) Level 3 Events Specification
The definition of 'EventTarget' in that specification.
Obsolete 매개변수 약간이 이제 선택사항(listener) 또는 null 값을 받아들임(useCapture).
Document Object Model (DOM) Level 2 Events Specification
The definition of 'EventTarget' in that specification.
Obsolete 초기 정의.

브라우저 호환성

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
EventTargetChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 6Opera Full support 7Safari Full support 1
Notes
Full support 1
Notes
Notes window.EventTarget did not exist on versions of Safari before 10.1.
WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1
Notes
Full support 1
Notes
Notes window.EventTarget did not exist on versions of Safari iOS before 10.3.
Samsung Internet Android Full support 1.0
EventTarget() constructorChrome Full support 64Edge Full support ≤79Firefox Full support 59IE No support NoOpera Full support 51Safari No support NoWebView Android Full support 64Chrome Android Full support 64Firefox Android Full support 59Opera Android Full support 47Safari iOS No support NoSamsung Internet Android Full support 9.0
addEventListenerChrome Full support 1
Notes
Full support 1
Notes
Notes Before Chrome 49, the type and listener parameters were optional.
Edge Full support 12Firefox Full support 1IE Full support 9
Full support 9
No support 6 — 11
Notes Alternate Name
Notes Older versions of IE supported an equivalent, proprietary EventTarget.attachEvent() method.
Alternate Name Uses the non-standard name: attachEvent
Opera Full support 7Safari Full support 1WebView Android Full support 1
Notes
Full support 1
Notes
Notes Before Chrome 49, the type and listener parameters were optional.
Chrome Android Full support 18
Notes
Full support 18
Notes
Notes Before Chrome 49, the type and listener parameters were optional.
Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0
Notes
Full support 1.0
Notes
Notes Before Samsung Internet 5.0, the type and listener parameters were optional.
dispatchEventChrome Full support 4Edge Full support 12Firefox Full support 2IE Full support 9
Full support 9
No support 6 — 11
Notes Alternate Name
Notes Older versions of IE supported an equivalent, proprietary EventTarget.fireEvent() method.
Alternate Name Uses the non-standard name: fireEvent
Opera Full support 9Safari Full support 3.2WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support 1.0
removeEventListenerChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 9
Full support 9
No support 6 — 11
Notes Alternate Name
Notes Older versions of IE supported an equivalent, proprietary EventTarget.detachEvent() method.
Alternate Name Uses the non-standard name: detachEvent
Opera Full support 7Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.
Uses a non-standard name.
Uses a non-standard name.

같이 보기