← Back to JavaScript Course | Chapter 5: ES6+ Features | Lesson 8 of 12

JS का globalThis

globalThis एक ऐसा नाम है जो हमेशा top-level shared space को point करता है, चाहे JavaScript कहीं भी चल रहा हो। यह एक master key जैसा है जो हर building में काम करती है।
Syntax
javascript
globalThis.propertyName = value;
globalThis.propertyName;

globalThis क्या है

globalThis एक standard property है जो global object को refer करती है चाहे code किसी भी JavaScript environment में चले, जो पहले हर platform पर अलग-अलग नामों की मांग करता था उसे एक कर देती है।

उदाहरण: What Is globalThis

javascript
console.log(typeof globalThis); // "object" in every environment

Node.js में globalThis

Node.js में, globalThis उसी global object को point करता है जिसे आप वरना global के रूप में access करते, यह Node code को बिना किसी Node-specific identifier के global state refer करने का एक तरीका देता है जो browser में काम नहीं करेगा।

उदाहरण: globalThis in Node.js

javascript
// In Node.js: globalThis === global
console.log(typeof globalThis);

globalThis और Browser Code

browsers में, globalThis उसी object को refer करता है जो window है (या किसी worker के अंदर self), इसलिए browser code किसी खास browser global के मौजूद होने का मान लिए बिना इसे इस्तेमाल कर सकता है।

उदाहरण: globalThis and Browser Code

javascript
// In browsers: globalThis === window
console.log(globalThis === window);

Portable Global Access

चूंकि globalThis हर जगह बिल्कुल एक जैसा काम करता है, global object की जांच करने या उससे जुड़ने वाला code — जैसे कोई polyfill — environment-detection branching के बिना ऐसा कर सकता है।

उदाहरण: Portable Global Access

javascript
// Check whether `!globalThis.myPolyfill`
// Check whether `!globalThis.myPolyfill`
if (!globalThis.myPolyfill) {
  // Assign `function () { return "polyfilled"; }` to `globalThis.myPolyfill`
  // Assign `function () { return "polyfilled"; }` to `globalThis.myPolyfill`
  globalThis.myPolyfill = function () { return "polyfilled"; };
}
// Print `globalThis.myPolyfill()` to the console
// Print `globalThis.myPolyfill()` to the console
console.log(globalThis.myPolyfill());

globalThis के साथ Best Practices

globalThis को सीधे छूने के बजाय explicit imports और modules को prefer करें; इसे सिर्फ genuine cross-environment library code के लिए रखें जिसे global object के साथ सुरक्षित रूप से interact करना ही हो।

उदाहरण: Best Practices with globalThis

javascript
// Prefer modules/imports over touching globalThis directly
import { helper } from './utils.js';
console.log(helper());
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. ऐसे code में window इस्तेमाल करना जो Node.js में भी चलता है, जो ReferenceError throw करता है; globalThis इस्तेमाल करें।
  2. globalThis पर गलती से global properties बना देना, जो दूसरे scripts से clash कर सकती हैं।
  3. यह उम्मीद करना कि top level पर let और const globalThis पर दिखेंगे, जबकि सिर्फ var और function declarations ही दिख सकते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.