Why globalThis instead of something intuitive like globalObject, systemGlobal, or globalEntity?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Why globalThis instead of something intuitive like globalObject, systemGlobal, or globalEntity?

#!/JoePea
I know it may be too late for this, but I imagine `globalThis` can bring confusion to beginners, especially ESL beginners.

Things like `globalObject`, `systemGlobal`, or `globalEntity` would've been more intuitive; they make sense in plain English.

Well anyways, have a good weekend!

- Joe



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Why globalThis instead of something intuitive like globalObject, systemGlobal, or globalEntity?

Jordan Harband
This question is more appropriate for the proposal repo, in which a very lengthy naming document will hopefully answer all of your questions: https://github.com/tc39/proposal-global/blob/master/NAMING.md

Please address all further comments or replies on this topic to the proposal repo. Thanks!

On Sat, Sep 21, 2019 at 11:12 AM #!/JoePea <[hidden email]> wrote:
I know it may be too late for this, but I imagine `globalThis` can bring confusion to beginners, especially ESL beginners.

Things like `globalObject`, `systemGlobal`, or `globalEntity` would've been more intuitive; they make sense in plain English.

Well anyways, have a good weekend!

- Joe


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Why globalThis instead of something intuitive like globalObject, systemGlobal, or globalEntity?

Isiah Meadows-2
In reply to this post by #!/JoePea
If you look in the proposal's repo, they explain the rationale behind the identifier: 

Here's a short summary: `globalThis` is the same value you get from evaluating `this` in the global scope of a sloppy mode script. This is subtly different than a "global object", which JS has no concept of. There's other nuances involved, too.

Concretely, for one example, `globalObject` was explicitly rejected because in HTML, the global `this` is a proxy of `window`, not the `window` object itself. The `window` proxy delegates all operations to the current global `window`, as you might expect. But here's where things get interesting: during navigation, the window changes, yet the global `this` does not, so if you define a property, capture the global `this`, and navigate, the property you just defined will disappear, but the global `this` you captured still has the same identity as the new reference.

This file in particular explains the naming side of it in detail: 
https://github.com/tc39/proposal-global/blob/master/NAMING.md It covers the other suggested names here indirectly.

And do keep in mind much of TC39's members are non-native speakers themselves, including one of the proposal's biggest champions/fans, Matthias. So it's not like that concern goes unnoticed - if anything, it gets accounted for without them even thinking about it. It might also explain why it's `array.entries()`, `map.entries()`, and similar when the more proper English name would've been `array.pairs()`, `map.pairs()`, and so on: entries are really what values from `value[Symbol.iterator]()` represent, and non-native speakers sometimes misuse "entries" as if it were synonymous with "pairs". Contrast this with Underscore's `_.pairs`, named by a native English speaker back in 2012, long before ES6 was a thing, and it stood as precedent for having `coll.entries()` for various collections.

On Sat, Sep 21, 2019 at 14:12 #!/JoePea <[hidden email]> wrote:
I know it may be too late for this, but I imagine `globalThis` can bring confusion to beginners, especially ESL beginners.

Things like `globalObject`, `systemGlobal`, or `globalEntity` would've been more intuitive; they make sense in plain English.

Well anyways, have a good weekend!

- Joe


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Why globalThis instead of something intuitive like globalObject, systemGlobal, or globalEntity?

#!/JoePea
> but the global `this` you captured still has the same identity as the new reference.

Interesting, I never knew that. Do you have a code sample to show how
to detect or prove that?

On Sat, Sep 21, 2019 at 9:05 PM Isiah Meadows <[hidden email]> wrote:

>
> If you look in the proposal's repo, they explain the rationale behind the identifier:
> https://github.com/tc39/proposal-global/
>
> Here's a short summary: `globalThis` is the same value you get from evaluating `this` in the global scope of a sloppy mode script. This is subtly different than a "global object", which JS has no concept of. There's other nuances involved, too.
>
> Concretely, for one example, `globalObject` was explicitly rejected because in HTML, the global `this` is a proxy of `window`, not the `window` object itself. The `window` proxy delegates all operations to the current global `window`, as you might expect. But here's where things get interesting: during navigation, the window changes, yet the global `this` does not, so if you define a property, capture the global `this`, and navigate, the property you just defined will disappear, but the global `this` you captured still has the same identity as the new reference.
>
> This file in particular explains the naming side of it in detail:
> https://github.com/tc39/proposal-global/blob/master/NAMING.md It covers the other suggested names here indirectly.
>
> And do keep in mind much of TC39's members are non-native speakers themselves, including one of the proposal's biggest champions/fans, Matthias. So it's not like that concern goes unnoticed - if anything, it gets accounted for without them even thinking about it. It might also explain why it's `array.entries()`, `map.entries()`, and similar when the more proper English name would've been `array.pairs()`, `map.pairs()`, and so on: entries are really what values from `value[Symbol.iterator]()` represent, and non-native speakers sometimes misuse "entries" as if it were synonymous with "pairs". Contrast this with Underscore's `_.pairs`, named by a native English speaker back in 2012, long before ES6 was a thing, and it stood as precedent for having `coll.entries()` for various collections.
>
> On Sat, Sep 21, 2019 at 14:12 #!/JoePea <[hidden email]> wrote:
>>
>> I know it may be too late for this, but I imagine `globalThis` can bring confusion to beginners, especially ESL beginners.
>>
>> Things like `globalObject`, `systemGlobal`, or `globalEntity` would've been more intuitive; they make sense in plain English.
>>
>> Well anyways, have a good weekend!
>>
>> - Joe
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Why globalThis instead of something intuitive like globalObject, systemGlobal, or globalEntity?

Boris Zbarsky
On 10/4/19 2:39 PM, #!/JoePea wrote:
>> but the global `this` you captured still has the same identity as the new reference.
>
> Interesting, I never knew that. Do you have a code sample to show how
> to detect or prove that?

Sure.  The following code logs "true, 5, 5, 5, 5, true, undefined,
undefined, undefined, 5" in Firefox, Chrome, and Safari, which shows
both the identity staying the same and the property disappearing from
the WindowProxy, as well as showing the difference between the
WindowProxy and the Window (the bareword lookup finds the var on the
global, which is the Window, while `self.something` goes through the
WindowProxy, which now points to the new `Window`):

<!DOCTYPE html>
<body>
   <script>
     var state = "first-load";
     var cachedThis;
     var cachedValueGetter;
     var cachedBarewordGetter;
     function loadHappened(iframe) {
       var win = iframe.contentWindow;

       if (state == "first-load") {
         cachedThis = win.getGlobalThis();
         cachedValueGetter = win.getPropertyValue;
         cachedBarewordGetter = win.getBareword;
         console.log(win == cachedThis);
         console.log(win.something);
         console.log(cachedThis.something);
         console.log(cachedValueGetter());
         console.log(cachedBarewordGetter());
         state = "second-load";
         iframe.srcdoc = iframe.srcdoc.replace("var something = 5;", "");
         return;
       }

       console.log(frames[0] == cachedThis);
       console.log(frames[0].something);
       console.log(cachedThis.something);
       console.log(cachedValueGetter());
       console.log(cachedBarewordGetter());
     }
   </script>
   <iframe srcdoc="
      <script>
        var self = this;
        var something = 5;
        function getGlobalThis() {
          return self;
        }
        function getPropertyValue() {
          return self.something;
        }
        function getBareword() {
          return something;
        }
      </script>"
      onload="loadHappened(this)"></iframe>
</body>

-Boris
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Why globalThis instead of something intuitive like globalObject, systemGlobal, or globalEntity?

#!/JoePea
Who's going to write code like that anyways?

Everyone's moving to ES modules, where `this` for accessing the global
is just `undefined`. It's highly unlikely that any concept other than
"global object" will ever be conceived by the vast majority of users,
especially future users.

On Fri, Oct 4, 2019 at 12:33 PM Boris Zbarsky <[hidden email]> wrote:

>
> On 10/4/19 2:39 PM, #!/JoePea wrote:
> >> but the global `this` you captured still has the same identity as the new reference.
> >
> > Interesting, I never knew that. Do you have a code sample to show how
> > to detect or prove that?
>
> Sure.  The following code logs "true, 5, 5, 5, 5, true, undefined,
> undefined, undefined, 5" in Firefox, Chrome, and Safari, which shows
> both the identity staying the same and the property disappearing from
> the WindowProxy, as well as showing the difference between the
> WindowProxy and the Window (the bareword lookup finds the var on the
> global, which is the Window, while `self.something` goes through the
> WindowProxy, which now points to the new `Window`):
>
> <!DOCTYPE html>
> <body>
>    <script>
>      var state = "first-load";
>      var cachedThis;
>      var cachedValueGetter;
>      var cachedBarewordGetter;
>      function loadHappened(iframe) {
>        var win = iframe.contentWindow;
>
>        if (state == "first-load") {
>          cachedThis = win.getGlobalThis();
>          cachedValueGetter = win.getPropertyValue;
>          cachedBarewordGetter = win.getBareword;
>          console.log(win == cachedThis);
>          console.log(win.something);
>          console.log(cachedThis.something);
>          console.log(cachedValueGetter());
>          console.log(cachedBarewordGetter());
>          state = "second-load";
>          iframe.srcdoc = iframe.srcdoc.replace("var something = 5;", "");
>          return;
>        }
>
>        console.log(frames[0] == cachedThis);
>        console.log(frames[0].something);
>        console.log(cachedThis.something);
>        console.log(cachedValueGetter());
>        console.log(cachedBarewordGetter());
>      }
>    </script>
>    <iframe srcdoc="
>       <script>
>         var self = this;
>         var something = 5;
>         function getGlobalThis() {
>           return self;
>         }
>         function getPropertyValue() {
>           return self.something;
>         }
>         function getBareword() {
>           return something;
>         }
>       </script>"
>       onload="loadHappened(this)"></iframe>
> </body>
>
> -Boris
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Why globalThis instead of something intuitive like globalObject, systemGlobal, or globalEntity?

Boris Zbarsky
On 10/7/19 1:56 PM, #!/JoePea wrote:
> Who's going to write code like that anyways?

In the general form of "function from a navigated-away-from document
runs because it's called by some script from outside the document", I've
seen this come up a number of times in web browser bug reports.

> Everyone's moving to ES modules, where `this` for accessing the global
> is just `undefined`.

It's not just "this".  The same thing applies to "window", "self", etc,
etc.  There is no way to actually get your hands on the global object
explicitly in a DOM Window scope: you always get the WindowProxy instead.

> It's highly unlikely that any concept other than
> "global object" will ever be conceived by the vast majority of users,
> especially future users.

Well, the thing that globalThis returns is NOT the global object, and
telling people that it is will just confuse them when it behaves in a
way they don't expect.

-Boris
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Why globalThis instead of something intuitive like globalObject, systemGlobal, or globalEntity?

#!/JoePea
> Well, the thing that globalThis returns is NOT the global object, and
telling people that it is will just confuse them when it behaves in a
way they don't expect.

Hello Boris, thanks for your opinion and thoughts!

That's the thing: the only people that may get confused are Ecmascript
and JS-Engine experts who know the internals very intimately. I've
been making web applications for a long time, and have never had to
think about the concept that you've described. I continue to believe
that *the vast majority* of web developers would never get confused,
and for those people that could possibly get confused, it would be
because they are familiar with browser internals (or hit a browser
*bug* like you describe), and in the first case they actually won't be
confused because they are familiar with engine internals, and in the
second case browsers need to not create bugs like that.

For all intents and purpose, every developer I've talked to, and every
article I've read, treats things like `window` as the global object.
It's as simple as that 99.999999% of the time.

I strongly believe that things should be made intuitive for the vast
majority of users, not the very few experts who know enough not to be
confused, or the very few people hitting a browser bug that should in
all honesty be patched in effort to make things easy for end
developers.

All the best,
- Joe

On Mon, Oct 7, 2019 at 1:11 PM Boris Zbarsky <[hidden email]> wrote:

>
> On 10/7/19 1:56 PM, #!/JoePea wrote:
> > Who's going to write code like that anyways?
>
> In the general form of "function from a navigated-away-from document
> runs because it's called by some script from outside the document", I've
> seen this come up a number of times in web browser bug reports.
>
> > Everyone's moving to ES modules, where `this` for accessing the global
> > is just `undefined`.
>
> It's not just "this".  The same thing applies to "window", "self", etc,
> etc.  There is no way to actually get your hands on the global object
> explicitly in a DOM Window scope: you always get the WindowProxy instead.
>
> > It's highly unlikely that any concept other than
> > "global object" will ever be conceived by the vast majority of users,
> > especially future users.
>
> Well, the thing that globalThis returns is NOT the global object, and
> telling people that it is will just confuse them when it behaves in a
> way they don't expect.
>
> -Boris
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss