Created
February 6, 2019 05:21
-
-
Save tonkotsuboy/7a008521bf413ecbf238ef1769d69ee8 to your computer and use it in GitHub Desktop.
EventTargetのクラス継承可能化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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