From d6362d1fc7621433ae41d7425d98281495f1877d Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 16 Sep 2019 23:43:14 +1000 Subject: [PATCH] src/js.js: new globalThis compat routine --- src/js.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/js.js b/src/js.js index c186893..bd17629 100644 --- a/src/js.js +++ b/src/js.js @@ -89,21 +89,25 @@ if (typeof process !== "undefined") { } catch (e) {} } -const global_env = (function() { - if (typeof process !== "undefined") { - /* node */ - return global; - } else if (typeof window !== "undefined") { - /* browser window */ - return window; - } else if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) { - /* web worker */ - return self; - } else { - /* unknown global env */ - return (0, eval)('this'); /* use non-strict mode to get global env */ - } -})(); +const global_env = (function(Object) { + return typeof globalThis === 'object' ? + globalThis // eslint-disable-line no-undef + : (this ? + // compat: strict mode is unsupported + this + : ( // Based on https://mathiasbynens.be/notes/globalthis + Object.defineProperty(Object.prototype, '_', { + configurable: true, + get: function () { + delete Object.prototype._; + return this; + } + }) + /* global _ */ + , _ + ) + ); +}(Object)); let apply, construct, Reflect_deleteProperty; if (typeof Reflect !== "undefined") {