跳到主要内容

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`');
};