> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/scope/global/object/this.md).

# globalThis

[JS](/web/js.md) ⟩ [scope](/web/js/scope.md) ⟩ [global](/web/js/scope/global.md) ⟩ [object](/web/js/scope/global/object.md) ⟩ globalThis

{% hint style="success" %}
([⭐️ ES2020](/web/js/feature/es2020.md))

[identifier](/web/js/grammar/token/id.md) for the [global object](/web/js/scope/global/object.md), <mark style="color:orange;">**regardless**</mark> of the <mark style="color:yellow;">**current**</mark> [context](/web/js/concept/context.md) and [environment](/web/js/concept/env.md).
{% endhint %}

{% tabs %}
{% tab title="💈範例" %}

* replit：[theGlobalObject](https://replit.com/@pegasusroe/theGlobalObject#index.js)

```javascript
// define a cross-environment polyfill that's safer 
// across pre-globalThis JS environments
const theGlobalObject =
    
    (typeof globalThis !== "undefined") ? globalThis :    // ES2020
    (typeof global     !== "undefined") ? global     :    // Node.js
    (typeof window     !== "undefined") ? window     :    // browser
    (typeof self       !== "undefined") ? self       :    // web worker

    // • such a function will automatically be run in "non-strict mode" 
    //   when invoked with the normal () function invocation.
    // • its `this` will point at the "global object".
    (new Function("return this"))();                      // other environments?
```

{% endtab %}

{% tab title="📗 參考" %}

* [ ] [YDKJS: Scope & Closures (v.2)](/web/master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2.md) ⟩ Ch. 4 ⟩ [GlobalThis](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch4.md#global-this)
  {% endtab %}
  {% endtabs %}
