Last active
March 7, 2020 13:59
-
-
Save petamoriken/bb99ca136bc663d3c6ac to your computer and use it in GitHub Desktop.
EventTarget constructor が使えないときに無理矢理 EventTarget クラスを作るコード。
This file contains hidden or 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
class MyEventTarget extends DocumentFragment { | |
constructor() { | |
super(); | |
Object.setPrototypeOf(this, EventTarget.prototype); | |
} | |
static [Symbol.hasInstance](instance) { | |
return instance instanceof EventTarget; | |
} | |
} |
const source = `
for (const key of Object.getOwnPropertyNames(self)) {
const target = self[key];
if (!(target instanceof Function)) { continue; }
if (target.prototype == null) { continue; }
if (!(target.prototype instanceof EventTarget)) { continue; }
try {
new target();
console.log(key);
} catch {}
}
`;
const blob = new Blob([source], { type: "text/javascript" });
const url = URL.createObjectURL(blob);
new Worker(url);
Worker 内だと FileReader
かな。
Feature Detection 付きの新しい方。
https://gist.github.com/petamoriken/d3745051d6f7684af58985d18efa2786
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
一応どれが速いか計測してみた。
で一覧を取得して jsPerf に投げてみた。
https://jsperf.com/eventtarget
DocumentFragment
とComment
とText
あたりが速そうだった。