[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/globalThis [Back]  [Original]

globalThis - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

globalThis

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2020 ..

globalThis this, .

In this article

function canMakeHTTPRequest() {
  return typeof globalThis.XMLHttpRequest === "function";
}

console.log(canMakeHTTPRequest());
// Expected output (in a browser): true

globalThis

, JavaScript. window, self frames, Worker self. Node.js , global.

this . this undefined. Function('return this')(), , eval(), CSP , Function .

globalThis this ( , ) . , window self, , window, , . . , , this globalThis.

HTML WindowProxy

globalThis , - - iframe cross-window Proxy ( ). , .

, self global - , . .

globalThis - Function('return this')(). , CSP . , es6-shim :

js
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");
};

var globals = getGlobal();

if (typeof globals.setTimeout !== "function") {
  //      setTimeout!
}

globalThis :

js
if (typeof globalThis.setTimeout !== "function") {
  //      setTimeout!
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-globalthis


Web Proxy Viewer  |  New URL  |  Original Page