Skip to content

Instantly share code, notes, and snippets.

@petamoriken
Last active March 8, 2020 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petamoriken/d3745051d6f7684af58985d18efa2786 to your computer and use it in GitHub Desktop.
Save petamoriken/d3745051d6f7684af58985d18efa2786 to your computer and use it in GitHub Desktop.
fallback EventTarget constructor for Safari <= 13 and IE
const Original = self.EventTarget;
let availabilityOfEventTargetConstructor = true;
try {
new Original();
} catch {
availabilityOfEventTargetConstructor = false;
}
/** @type {{ new(): EventTarget, prototype: EventTarget }} */
export let EventTarget;
if (availabilityOfEventTargetConstructor) {
EventTarget = Original;
} else {
/** @type {{ new (): EventTarget, prototype: EventTarget }} */
// @ts-ignore
const FallbackEventTarget = function EventTarget() {
if (new.target === undefined) {
throw new TypeError("Constructor EventTarget requires 'new'");
}
/** @type {EventTarget} */
const fallback = self?.document.createDocumentFragment() ?? new FileReader();
Object.setPrototypeOf(fallback, Original.prototype);
return fallback;
}
FallbackEventTarget.prototype = Original.prototype;
EventTarget = FallbackEventTarget;
}
@petamoriken
Copy link
Author

@petamoriken
Copy link
Author

petamoriken commented Mar 7, 2020

@petamoriken
Copy link
Author

こちらの記事を参考にしました。
http://var.blog.jp/archives/81507050.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment