Skip to content

Instantly share code, notes, and snippets.

@mrjacobbloom
Last active June 25, 2019 15:56
Show Gist options
  • Save mrjacobbloom/c75059af97d9d0e7d350b35b2d6af572 to your computer and use it in GitHub Desktop.
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.
// 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