Last active
June 25, 2019 15:56
-
-
Save mrjacobbloom/c75059af97d9d0e7d350b35b2d6af572 to your computer and use it in GitHub Desktop.
Polyfill to add support for the global Undefined object to browsers that do not yet support it.
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
// Polyfill to add support for the global Undefined object to browsers that do not yet support it. | |
// Released into the public domain by Jacob Bloom | |
(() => { | |
const global_ = typeof globalThis !== 'undefined' ? globalThis : | |
typeof self !== 'undefined' ? self : | |
typeof window !== 'undefined' ? window : | |
typeof global !== 'undefined' ? global : null; | |
if (global_ === null) throw new Error('Cannot polyfill Undefined: unable to locate global object'); | |
if('Undefined' in global_) return; | |
function Undefined(value) { | |
if(new.target) { | |
this['[[PrimitiveValue]]'] = Undefined(value); | |
} else { | |
return void value; | |
} | |
} | |
Undefined.toString = function toString() { return 'function Undefined() { [native code] }'; }; | |
Undefined.isUndefined = function isUndefined(value) { return Object.is(value, Undefined(value)); }; | |
Undefined.prototype.valueOf = function valueOf() { return this['[[PrimitiveValue]]']; }; | |
Undefined.prototype.toString = function toString() { return 'undefined'; }; | |
Undefined.prototype.toNull = function toNull() { return null; }; | |
if(typeof window !== 'undefined') { | |
Undefined.prototype.toHTMLAllCollection = function toHTMLAllCollection() { return window.document.all; }; | |
} | |
global_.Undefined = Undefined; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment