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
Parameters
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;
myEventTarget.addEventListener("foo", (e) => {
myEventTarget._secret = e.detail;
});
let event = new CustomEvent("foo", { detail: 7 });
myEventTarget.dispatchEvent(event);
let newValue = myEventTarget.secret;
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