W3cubDocs

/Web APIs

EventTarget: EventTarget() constructor

The EventTarget() constructor creates a new EventTarget object instance.

Note: It is fairly rare to explicitly call this constructor. Most of the time, this constructor is used inside the constructor of an object extending the EventTarget interface, using the super keyword.

Syntax

js

new EventTarget()

Parameters

None.

Return value

A new instance of the EventTarget object.

Examples

js

class MyEventTarget extends EventTarget {
  constructor(mySecret) {
    super();
    this._secret = mySecret;
  }

  get secret() {
    return this._secret;
  }
}

let myEventTarget = new MyEventTarget(5);
let value = myEventTarget.secret; // === 5
myEventTarget.addEventListener("foo", (e) => {
  myEventTarget._secret = e.detail;
});

let event = new CustomEvent("foo", { detail: 7 });
myEventTarget.dispatchEvent(event);
let newValue = myEventTarget.secret; // === 7

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
EventTarget 64 79 59 No 51 14 64 64 59 47 14 9.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/EventTarget