Esta página foi traduzida do inglês pela comunidade.Saiba mais e junte-se à comunidade MDN Web Docs.
EventTarget()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since setembro de 2020.
O construtorEventTarget() cria uma nova instância do objetoEventTarget.
In this article
Sintaxe
var myEventTarget = new EventTarget();
Parâmetros
Nenhum.
Retorno de valor
Uma instância do objetoEventTarget.
Exemplos
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; // == 5myEventTarget.addEventListener("foo", function (e) { this._secret = e.detail;});let event = new CustomEvent("foo", { detail: 7 });myEventTarget.dispatchEvent(event);let newValue = myEventTarget.secret; // == 7Especificações
| Specification |
|---|
| DOM> # ref-for-dom-eventtarget-eventtarget①> |