-
Notifications
You must be signed in to change notification settings - Fork 22.5k
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
Use core-js for getGlobals example #17337
Conversation
Preview URLsFlawsNone! 🎉 External URLsURL:
(this comment was updated 2022-08-25 18:32:52.159186) |
It will fail in some cases. See https://github.com/zloirock/core-js/blob/master/packages/core-js/internals/global.js and zloirock/core-js#86 (comment) |
Should we point to core-js then? We seem to use core-js links in more places. |
As you wish. I just pointed out that |
Yes—I'm not sure about the editors' opinions on this, so just asking :) |
@wbamberg What do you think here? Should we change the link and the code example to |
@Josh-Cena , I'd be happy to use core-js here instead, if you want to make that change :). |
@zloirock One thing I'm unsure of: why is it necessary to check for |
@Josh-Cena the first |
Ah, sorry, I see now. Thanks! |
(function () { return this; })() || | ||
Function('return this')(); | ||
|
||
if (typeof globalObject.setTimeout !== 'function') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can check typeof setTimeout
without globalObject
, so I don't think that it's a good example. However, I'm not sure that it's in the scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just remove it—we are showing globalThis
anyway.
I realized that by removing it we are essentially providing a polyfill😅 Do you have a better use-case in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me, it's for operations with global variables that are more complex than a simple check. For example,
if (!globalObject.Promise) {
Object.defineProperty(globalObject, 'Promise', {
value: function Promise () {
// your Promise implementation
},
enumerable: false,
configurable: true,
writable: true
});
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that's very useful inspiration—decided to use Intl
instead of Promise
so it looks more realistic.
👋 Anyone to review this? @wbamberg? |
Trying to summon @wbamberg again since this one is quite small |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to be slow @Josh-Cena .
files/en-us/web/javascript/reference/global_objects/globalthis/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/javascript/reference/global_objects/globalthis/index.md
Outdated
Show resolved
Hide resolved
1f95740
to
5ce59ab
Compare
019f599
to
3fe256a
Compare
3fe256a
to
228fcc0
Compare
check(typeof global === 'object' && global) || | ||
// This returns undefined when running in strict mode | ||
(function () { return this; })() || | ||
Function('return this')(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now, without globalThis
, it will not work in cases of ES-only environments (no one of the variables above) in modules context (IIFE this
is undefined
), where for security reasons disabled evaluation via eval
/ Function
. It's not a big problem to create such an environment, for example, with NodeJS vm
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right—the purpose of this example is to illustrate what happens without globalThis
. I did dig into the history of internals/global.js
and it was something like this a long time ago: https://github.com/zloirock/core-js/blob/b4e8c0b09bbe23db8d8784df03ef0b8f3ff04170/packages/core-js/internals/global.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And after that, someone wrote me an issue about such an environment.
Someone could try to use the code from this example as a universal way of getting the global object and theoretically, it could fail in this specific and rare case. So I think changing "prior to globalThis
..." to something else could be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only thing I've modified from the core-JS source is removal of the globalThis
mention. In a runtime without globalThis
, is there anything better than this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean that someone could try this code everywhere as something universal - in environments without globalThis
and in environments with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh well, it was kind of the same code before—let's hope no one uses it as a polyfill (they shouldn't!) If you want to add a warning box making it explicit, feel free to.
files/en-us/web/javascript/reference/global_objects/globalthis/index.md
Outdated
Show resolved
Hide resolved
…/index.md Co-authored-by: wbamberg <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 thanks for your patience @Josh-Cena !
Summary
Motivation
Fix #10470. I hope this can reduce the confusion.
Also reverted back from
const
tovar
because this is verbatim-copied from the original code. Let me know if we still want to useconst
.Supporting details
Related issues
Metadata