Last active
March 8, 2020 14:08
-
-
Save petamoriken/d3745051d6f7684af58985d18efa2786 to your computer and use it in GitHub Desktop.
fallback EventTarget constructor for Safari <= 13 and IE
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
以前のやつ。
https://gist.github.com/petamoriken/bb99ca136bc663d3c6ac