DEV Community

Laurie
Laurie

Posted on

28 8

globalThis is coming to JavaScript

One of the first things we learn with JavaScript is the keyword this. What is tricky to understand at first is that its definition is always different. It depends on the scope we're accessing it in.

Well, in all projects there is a "global" this. But it's called something different depending on what context you're in. On the web you may be familiar with it as the window object. In other contexts it's self and sometimes it's this!

As it turns out, there is a function that's been refined over time to always access it.

var getGlobal = function () {
    if (typeof self !== 'undefined') { return self; }
    if (typeof window !== 'undefined') { return window; }
    if (typeof global !== 'undefined') { return global; }
    throw new Error('unable to locate global object');
};
Enter fullscreen mode Exit fullscreen mode

Not exactly pretty. And a pain to include in every project.

But no more! Now at stage 4, globalThis is the latest addition to ECMAScript.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (10)

Collapse
 
ben profile image
Ben Halpern β€’

Nobody will ever be happy with the name globalThis πŸ˜‚

Seems really useful though!

Collapse
 
nicklvsa profile image
Nick β€’

Yeaaa... usefulness is an 11/10 but name scheme is about a -1/10

Collapse
 
mausworks profile image
Rasmus WennerstrΓΆm β€’

I don't see why they couldn't just name it global. There's the "let's not conflict"-argument, and "let's fight around with existing types".

But in all honesty... there's not much you can "assume" about the global object anyways, you will still have to check if (global.feature) for most features if you're building a cross-platform lib, and I'm not sure how globalThis would help with that.

Collapse
 
laurieontech profile image
Laurie β€’

"Attempts were made to ship under the name global, but it turns out that this does, in fact, break some existing websites." per the spec github.com/tc39/proposal-global

Thread Thread
 
mausworks profile image
Rasmus WennerstrΓΆm β€’

Thank you for sharing! ✌️

Collapse
 
seanmclem profile image
Seanmclem β€’

What might be a use-case where I would want a globalThis instead of just manually accessing window, or this?

Collapse
 
laurieontech profile image
Laurie β€’ β€’ Edited

It's a standard. So it will work consistently regardless of what environment the code is run in.

Collapse
 
mausworks profile image
Rasmus WennerstrΓΆm β€’

"As consistent as it can get".

Different global objects will expose very different APIs, so now instead of having to check if (typeof global !== 'undefined') global.nodeFeature(), you can now check:

if (globalThis.nodeFeature) { 
  globalThis.nodeFeature();
} else if (globalThis.webFeature) { 
  globalThis.webFeature();
}

I think it's useful, but it's just kind of deferring the problem, as you will still have to write platform-specific abstractions.

Thread Thread
 
seanmclem profile image
Seanmclem β€’

Yeah I guess that was my concern. If the different 'this' you're targeting can have radically different APIs, I would wonder why target them with a single globalThis instead of individually. I figured I was missing something..

Collapse
 
jenc profile image
Jen Chan β€’

Ugh I wish I had that func you just offered up my sleeve

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay