globalThis is not defined
时间: 2023-08-28 22:04:48 浏览: 96
The `globalThis` keyword was introduced in ECMAScript 2020 and is used to refer to the global object in a consistent way across different environments (e.g. browser, Node.js). However, it may not be supported in older browsers or environments that have not yet implemented the ECMAScript 2020 specification.
To use `globalThis` in environments that do not support it, you can use a polyfill or fallback to other methods of accessing the global object, such as `window` in a browser or `global` in Node.js. For example:
```
const theGlobalObject = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : this;
```
This code snippet checks for the existence of `globalThis`, `window`, `global`, and `this` in that order, and assigns the first one that is defined to the `theGlobalObject` variable.