跳转到内容

useGlobalThis

此内容尚不支持你的语言。

biome.json
{
"linter": {
"rules": {
"nursery": {
"useGlobalThis": "error"
}
}
}
}

Enforce the use of globalThis over window, self, and global.

globalThis is a standard way to access the global object across platforms such as browsers, Web Workers, Node.js and so on, and using it can make your code portable.

However, there are several exceptions that are allowed:

  1. Certain window/Web Workers-specific APIs, such as window.innerHeight and self.postMessage
  2. Window-specific events, such as window.addEventListener('resize')
window.foo;
code-block.js:1:1 lint/nursery/useGlobalThis ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer globalThis over window, self and global.

> 1 │ window.foo;
^^^^^^
2 │

globalThis is the standard way to access the global object across environments, which improves code portability.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

window.addEventListener('click', () => {});
code-block.js:1:1 lint/nursery/useGlobalThis ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer globalThis over window, self and global.

> 1 │ window.addEventListener(‘click’, () => {});
^^^^^^
2 │

globalThis is the standard way to access the global object across environments, which improves code portability.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

globalThis.foo;
globalThis.addEventListener('click', () => {});
// window/Web Workers-specific APIs are allowed
window.innerWidth;
self.postMessage({ type: 'ready' });
// window-specific events are allowed
window.addEventListener('resize', () => {});