It looks like the spec doesn't mandate that the [[GlobalThisValue]] actually inherit from Object.prototype so this is *almost* universal but just not quite. It does make me wonder if a truly universal method exists or not. https://tc39.github.io/ecma262/#table-18 …
-
-
-
It does though, in the exact section you link to: “Hosts may provide any ECMAScript Object value.” And all objects inherit from %ObjectPrototype%.
- 6 more replies
New conversation -
-
-
What are some uses for globalThis ?
-
Not that many use cases. Mostly useful for polyfills that need access to the global `this` for some reason.
@ljharb - 2 more replies
New conversation -
-
-
So `__magic__` is `this`, which i guess will always be window? then `__magic__.globalThis = __magic` essentially says `window.globalThis = window` ...and once it's set, you can delete `__magic__` sounds overly complicated
-
Importantly, `__magic__` is the global `this` (not just any `this`). If you know of an easier way to polyfill `globalThis` under the constraints mentioned in the article, I’d love to hear it.
- 2 more replies
New conversation -
-
-
If your code ends up being wrapped in a `with`, you probably have bigger problems though. Do you know of a packer/bundler tool that does this?
End of conversation
New conversation -
-
Clever! And a very useful write-up. But I think I’d prefer something more “traditional”, with eval() as a last resort (I wouldn’t want to rely on Object.prototype being in the chain of globalThis). E.g.:https://gist.github.com/rauschma/bb3e5047a461961cfe19a1335b772d7c …
-
Hmm, I’m not a fan of it either, but CSP does seem more common than an environment in which globalThis doesn’t inherit from Object.prototype — I don’t know of any modern ones.
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.
JavaScript, HTML, CSS, HTTP, performance, security, Bash, Unicode, i18n, macOS.
`globalThis` makes it easy to access the global `this` value in any JavaScript environment.
Surprisingly, polyfilling it correctly requires some arcane JavaScript wizardry.
A horrifying globalThis polyfill in 100% universal JavaScript:
