"Redacted" global-namespace

# kai zhu (6 years ago)

+1 for unified global-object (currently stage-3)

can someone pm me what the redacted-name is? i'm interested in polyfilling it for existing projects, e.g.:

/*jslint browser*/
/*global global process*/
(function () {
/*
 * polyfill for
 * https://github.com/tc39/proposal-global
 * assuming redacted-name is "global2"
 */
    "use strict";
    var global2;
    try {
        global2 = process.versions.node && global;
    } catch (ignore) {
    }
    global2 = global2 || window;
    global2.global2 = global2;
}());

kai zhu kaizhu256 at gmail.com

# Jordan Harband (6 years ago)

The polyfill already exists on npm www.npmjs.com/package/globalthis; it's linked from the proposal

repo.

# kai zhu (6 years ago)

thx. for those too lazy, i translated the npm-package into a standalone-snippet you can copy-paste anywhere:

/*jslint browser*/
/*global global global2 self*/
(function () {
/*
 * polyfill for
 * https://github.com/tc39/proposal-global
 * 1. replace all occurrences of global2 with <redacted-name>
 * 2. copy-paste this code into any browser/node/io.js/node-webkit script
 *    to enable global-object global2
 */
    "use strict";
    var descriptor;
    var obj;
    var polyfill;
    if (
        typeof global !== "object"
        || !global
        || global.Math !== Math
        || global.Array !== Array
    ) {
        if (String(typeof self) !== "undefined") {
            polyfill = self;
        } else if (String(typeof window) !== "undefined") {
            polyfill = window;
        } else if (String(typeof global) !== "undefined") {
            polyfill = global;
        } else {
            polyfill = Function('return this')(); // jslint ignore:line
        }
    } else {
        polyfill = global;
    }
    if (Object.defineProperty && (function () {
        obj = {};
        try {
            Object.defineProperty(obj, "x", {enumerable: false, value: obj});
/* jslint ignore:start */
            for (var _ in obj) {
                return false;
            }
/* jslint ignore:end */
            return obj.x === obj;
        } catch (ignore) { /* this is IE 8. */
            return false;
        }
    }())) {
        descriptor = Object.getOwnPropertyDescriptor(polyfill, "global2");
        if (!descriptor || (
            descriptor.configurable
            && (descriptor.enumerable || descriptor.writable || global2 !== polyfill)
        )) {
            Object.defineProperty(polyfill, "global2", {
                configurable: true,
                enumerable: false,
                value: polyfill,
                writable: false
            });
        }
    } else if (typeof global2 !== "object" || global2 !== polyfill) {
        polyfill.global2 = polyfill;
    }
}());

kai zhu kaizhu256 at gmail.com