Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: prefer-global-this #978

Closed
sindresorhus opened this issue Jan 1, 2021 · 7 comments · Fixed by #2410
Closed

Rule proposal: prefer-global-this #978

sindresorhus opened this issue Jan 1, 2021 · 7 comments · Fixed by #2410

Comments

@sindresorhus
Copy link
Owner

globalThis is the unified way to access the "global" object.

This has the benefit of making code more cross-platform and more consistent.

globalThis requires Node.js 12, so we cannot add it to the recommended preset until April 2021 (date of Node.js 10 obsoletion).

Fail

window.foo; // Browser

self.foo; // Web Worker

global.foo; // Node.js

Pass

globalThis.foo;
@sindresorhus
Copy link
Owner Author

This is now accepted.

@fregante
Copy link
Collaborator

fregante commented Jan 17, 2021

Can this auto fix to this when it's the same? "globalThis" is so awkward.

Also it should exclude window’s actual properties, like window.innerWidth

@sindresorhus
Copy link
Owner Author

Can this auto fix to this when it's the same? "globalThis" is so awkward.

In what situations are they the same?

Also it should exclude window’s actual properties, like window.innerWidth

Yes, good catch. A list can be found here: http://developer.mozilla.org/en-US/docs/Web/API/Window For those, it should enforce window over globalThis.

@fregante

This comment was marked as resolved.

@sindresorhus

This comment was marked as resolved.

@fregante
Copy link
Collaborator

fregante commented Jun 4, 2021

Changed my mind. globalThis is more clear 👍

@fregante
Copy link
Collaborator

fregante commented Mar 7, 2024

Also:

Object.assign(window, {something: true});

Object.defineProperty(window, 'something', {
  value: true,
  writable: true,
});

Object.defineProperties(window, {
  something: {
    value: true,
    writable: true,
  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants