Published
Edited
Jan 8, 2021
Insert cell
Insert cell
Insert cell
Insert cell
// from https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/EventTarget#Examples
class MyEventTarget extends EventTarget {
constructor(mySecret) {
super();
this._secret = mySecret;
}

get secret() {
return this._secret;
}
}
Insert cell
myEventTarget = new MyEventTarget(5)
Insert cell
myEventTarget.addEventListener("foo", function(e) {
this._secret = e.detail;
})
Insert cell
{
const valueBefore = myEventTarget.secret; // == 5
myEventTarget.dispatchEvent(new CustomEvent("foo", { detail: 7 }));
const valueAfter = myEventTarget.secret; // == 7
return { valueBefore, valueAfter };
}
Insert cell
Insert cell
myEventTarget._eventListeners
Insert cell
Insert cell
result = {
const target = new MyEventTarget(6);
target.addEventListener(
"bar",
function(e) {
this._secret = e.detail;
},
{ once: true }
);
const valueBefore = target.secret; // == 6
target.dispatchEvent(new CustomEvent("bar", { detail: 8 }));
target.dispatchEvent(new CustomEvent("bar", { detail: 9 }));
const valueAfter = target.secret; // == 8
return { valueBefore, valueAfter, target };
}
Insert cell
result.target._eventListeners // empty
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more