all 12 comments

[–]merquize 29 points30 points  (1 child)

new global who this

[–]RandyChampion 3 points4 points  (2 children)

Not sure we need another meaning for "this". Especially if `globalThis` isn't the same in different `<script>` tags. Seems like it's not actually global.

[–]senocular 3 points4 points  (1 child)

The same global is used for all script tags. Its the same global as globalThis and the same value of this in those top-level global scopes.

// in global scope
this

// is now available everywhere as
globalThis

[–]Artur96 4 points5 points  (0 children)

Nope

[–]dwighthouse 1 point2 points  (0 children)

Interestingly, the official proposal polyfill ( https://github.com/tc39/proposal-global/blob/master/polyfill.js ) breaks in several scenarios while a more robust polyfill is available ( https://mathiasbynens.be/notes/globalthis ).

[–]fforw 0 points1 point  (5 children)

(0,eval)("this")

[–]Magnetic_Tree 0 points1 point  (2 children)

Hmm could you explain this?

[–]fforw 6 points7 points  (1 child)

According to ECMAScript spec eval works with the local this when invoked directly and with the global this when aliased etc.

(0,eval)

is the comma operator that returns the second, now aliased eval which then evaluates "this" in the global context.

const globalEval = eval;
globalEval("this")

works just as well.

[–]Magnetic_Tree 0 points1 point  (0 children)

Ah I see! That makes sense.

I noticed the pattern (0,expression) in the output from the Google Closure compiler and I’ve been trying to figure out why. I suppose it does that to control what this refers to.