Skip to content

Instantly share code, notes, and snippets.

@tonkotsuboy
Created February 6, 2019 05:21
Show Gist options
  • Save tonkotsuboy/7a008521bf413ecbf238ef1769d69ee8 to your computer and use it in GitHub Desktop.
Save tonkotsuboy/7a008521bf413ecbf238ef1769d69ee8 to your computer and use it in GitHub Desktop.
EventTargetのクラス継承可能化
/** EventTargetを継承したクラス */
class MyClass extends EventTarget {
start() {
// 2秒後に「運命の出逢い」イベントを発生させる
setTimeout(() => {
this.dispatchEvent(new CustomEvent("運命の出逢い"));
}, 2000);
}
}
// ----------------
// MyClassの挙動確認
// ----------------
const myInstance = new MyClass();
// 「運命の出逢い」イベントをキャッチしたらログを出す
myInstance.addEventListener("運命の出逢い", () => {
console.warn("運命の出逢いが起こりました");
});
// MyClassのstart()メソッドを実行する
myInstance.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment