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

new EventTarget()

Parameters

None.

Return value

A new instance of the EventTarget object.

Examples

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

Specification
DOM Standard
# ref-for-dom-eventtarget-eventtarget①

Browser compatibility

BCD tables only load in the browser

See also