文章目录
返回不同环境下的this
module.exports = function () {
// globalThis:浏览器、node环境等任意环境使用
if (typeof globalThis === "object") {
return globalThis;
}
var g;
try {
g = this || new Function("return this")();// 在严格模式和模块环境下返回this
} catch (e) {
// 浏览器环境
if (typeof window === "object") {
return window;
} // This works if the self reference is available
// 浏览器环境、webWorker环境
if (typeof self === "object") {
return self;
} // This works if the global reference is available
// node环境
if (typeof global !== "undefined") {
return global;
}
}
return g;
}();