JavaScript 全局对象
约 1 分钟阅读
globals
- mdn globalThis
- Chrome 71+, Firefox 65+, Safari 12.1+, Node.js 12.0.0+
function getGlobals(): typeof globalThis | undefined {
if (typeof globalThis !== 'undefined') {
return globalThis;
} else if (typeof self !== 'undefined') {
return self;
} else if (typeof global !== 'undefined') {
return global;
}
return undefined;
}
export const globals = getGlobals();
export const getGlobalThis = (): typeof globalThis => {
if (typeof globalThis !== 'undefined') return globalThis;
if (typeof self !== 'undefined') return self;
if (typeof window !== 'undefined') return window;
if (typeof global !== 'undefined') return global as any;
if (typeof this !== 'undefined') return this as any;
throw new Error('Unable to locate global `this`');
};关联信息
反向链接和本文引用的外部资料。
References
其他外链
1 条- developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThisdeveloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis