Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (Ī²)
Service
Qiita JobsQiita ZineQiita Blog
61
Help us understand the problem. What is going on with this article?
@uhyo

šŸ‘»globalThisšŸ‘»ćØšŸŒglobalšŸŒćØšŸŒthisšŸŒ

More than 1 year has passed since last update.

ēš†ć•ć‚“恓悓恫恔ćÆć€‚ä»Šå›žćÆJavaScript恮šŸ‘»globalThisšŸ‘»ć«ć¤ć„ć¦č§£čŖ¬ć—ć‚ˆć†ćØę€ć„ć¾ć™ć€‚globalThisćÆJavaScript (ECMAScript) ć®ę–°ę©Ÿčƒ½ć§ć™ć€‚ē¾åœØTC39ćƒ—ćƒ­ć‚»ć‚¹ć®Stage 3恫恂悊1ć€ć“ć®ć¾ć¾ä½•äŗ‹ć‚‚ćŖ恑悌恰čæ‘恄恆恔恫ECMAScript2ć«ę­£å¼ęŽ”ē”Ø恕悌悋恓ćØ恫ćŖć‚Šć¾ć™ć€‚

恓恮čؘäŗ‹ćÆJavaScript2 Advent Calendar 2018恮5ę—„ē›®ć®čؘäŗ‹ć§ć™ć€‚

恕恦态ēš†ć•ć‚“ćÆ恓恮globalThis悒ä½æć£ćŸć“ćØ恌恂悋恧恗悇恆恋怂ē§ćÆć‚ć‚Šć¾ć›ć‚“ć€‚ćØ恄恆恋态ē¾åœØ恮ćØ恓悍globalThisćŒåˆ©ē”ØåÆčƒ½ćŖ恮ćÆć€ć¤ć„ä»Šę—„å®‰å®šē‰ˆćŒćƒŖćƒŖćƒ¼ć‚¹ć•ć‚ŒćŸć°ć‹ć‚Šć®Google Chrome 71恮ćæ恧恙怂恓恮čؘäŗ‹ć«ćÆglobalThis悒ä½æć£ćŸć‚³ćƒ¼ćƒ‰ä¾‹ćŒå‡ŗć¦ćć¾ć™ćŒć€å®Ÿéš›ć«å‹•ć‹ć—ćŸć„å “åˆćÆć”ć‚ƒć‚“ćØGoogle Chrome恌꜀ꖰē‰ˆć«ćŖć£ć¦ć„ć‚‹ć‹ē¢ŗčŖć—ć¦ćć ć•ć„ć­ć€‚
ļ¼ˆChrome 71恌ćƒŖćƒŖćƒ¼ć‚¹ć•ć‚Œć‚‹ć®ćÆę—„ęœ¬ę™‚é–“ć§ä»Šę—„ļ¼ˆ12꜈5ę—„ļ¼‰ć®ćÆ恚恧恙恌态恂ćŖćŸćŒć“ć®čؘäŗ‹ć‚’čŖ­ć‚€ć‚æć‚¤ćƒŸćƒ³ć‚°ć«ć‚ˆć£ć¦ćÆć¾ć å‡ŗ恦恄ćŖć„ć‹ć‚‚ć—ć‚Œć¾ć›ć‚“ć€‚ćć®å “åˆćÆå‡ŗć‚‹ć¾ć§å¾…ć¤ć‹ćƒ™ćƒ¼ć‚æē‰ˆć‚’ć‚¤ćƒ³ć‚¹ćƒˆćƒ¼ćƒ«ć—ć¦ćć ć•ć„ć€‚ļ¼‰

ā€» čæ½čؘļ¼ˆ2019-02-06ļ¼‰ļ¼šē¾åœØ恮ćØ恓悍态Google ChromećØFirefoxć®ęœ€ę–°ē‰ˆć§globalThisćŒć‚µćƒćƒ¼ćƒˆć•ć‚Œć¦ć„ć¾ć™ć€‚

ā€» čæ½čؘļ¼ˆ2019-04-26ļ¼‰ļ¼šē¾åœØ恮ćØ恓悍态Google Chrome惻Firefox惻Safarić®ęœ€ę–°ē‰ˆć€ćć—恦node.js v12恧globalThisćŒć‚µćƒćƒ¼ćƒˆć•ć‚Œć¦ć„ć¾ć™ć€‚

globalThisćØćÆä½•ć‹

äø€čØ€ć§ć„ć†ćØ态globalThisćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć§ć™ć€‚ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćØ恄恆恮ćÆć€ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć‚’ćƒ—ćƒ­ćƒ‘ćƒ†ć‚£ć«ęŒć¤ć‚ˆć†ćŖć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć§ć™ć€‚ć¤ć¾ć‚Šć€ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć‚’ä½œć‚‹ćØć€ćć‚ŒćŒć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®ćƒ—ćƒ­ćƒ‘ćƒ†ć‚£ćØćŖć£ć¦ē¾ć‚Œć‚‹ć®ć§ć™ć€‚

ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®ä¾‹
// ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°fooć‚’ä½œęˆļ¼ˆ
foo = 123;
// globalThis.foo恌123恫ćŖć£ć¦ć„ć‚‹
console.log(globalThis.foo); // 123

// 逆悂åÆčƒ½
globalThis.bar = 456;
console.log(bar); // 456

ä½™č«‡ć§ć™ćŒć€ä½•ć‚‚ä»˜ć‘ćŖ恄恧恄恍ćŖć‚Šä½œć£ćŸå¤‰ę•°ćØvar悒ä½æć£ć¦å®£čØ€ć—ćŸå¤‰ę•°ćÆć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć«ćŖć‚Šć¾ć™ćŒć€let悄constć§ä½œć£ćŸå¤‰ę•°ćÆć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć«ćŖć‚Šć¾ć›ć‚“ć€‚ć‚„ć£ć±ć‚Šlet悄constćÆå‰ć„ć§ć™ć­ć€‚

foo = 123;
console.log(globalThis.foo); // 123
var bar = 456;
console.log(globalThis.bar); // 456
let hoge = 7;
console.log(globalThis.hoge); // undefined
const fuga = 8;
console.log(globalThis.fuga); // undefined

ćƒ–ćƒ©ć‚¦ć‚¶ć«ćŠć‘ć‚‹ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈

ćØć“ć‚ć§ć€ć“ć“ć¾ć§čŖ­ć‚“恠äø€éƒØć®ę–¹ćÆ怌恂悌ļ¼Ÿć€ćØę€ć£ćŸć®ć§ćÆćŖ恄恧恗悇恆恋怂ćØ恄恆恮悂态ēš†ć•ć‚“ćŒćŠä½æć„ć®ćƒ–ćƒ©ć‚¦ć‚¶ć«ćÆę—¢ć«windowćŒå‚™ć‚ć£ć¦ć„ć¾ć™ć‚ˆć­ć€‚JavaScriptć§ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćØ恄恈恰window恮ćÆćšć§ć™ć€‚å®Ÿéš›ć€ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć‚’ä½œć‚‹ćØwindowć®ćƒ—ćƒ­ćƒ‘ćƒ†ć‚£ć«ćŖ悋恓ćØćÆå¤ćć‹ć‚‰ēŸ„ć‚‰ć‚Œć¦ć„ć¾ć™ć€‚

windowć®ä¾‹
foo = 123;

console.log(window.foo); // 123

ēØ®ć‚’ę˜Žć‹ć—ć¦ć—ć¾ć†ćØ态globalThisćÆwindowćØåŒć˜ć§ć™ć€‚

console.log(globalThis === window); // true

ćØćÆ恄恈态怌恘悃恂globalThisćØ恋恄悉ćŖ恄恘悃悓怍ćŖ恩ćØćÆä»Šć•ć‚‰ę€ć„ć¾ć›ć‚“ć‚ˆć­ć€‚windowćÆćƒ–ćƒ©ć‚¦ć‚¶ļ¼ˆDOMćŒå®Ÿč£…ć•ć‚Œć¦ć„ć‚‹ē’°å¢ƒļ¼‰ć«ć—ć‹ć‚ć‚Šć¾ć›ć‚“ćŒć€ä»Šć‚„JavaScriptćŒå‹•ä½œć™ć‚‹ē’°å¢ƒćÆ恝悌恠恑恧ćÆć‚ć‚Šć¾ć›ć‚“ć€‚

node.jsć«ćŠć‘ć‚‹ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈

ćƒ–ćƒ©ć‚¦ć‚¶ä»„å¤–ć®JavaScriptē’°å¢ƒćØć—ć¦ä»£č”Øēš„ćŖć®ćŒnode.js恧恙怂恓恮node.js恫ćÆwindowćÆ存åœØć—ć¾ć›ć‚“ć€‚

ćć®äø€ę–¹ć§ć€node.js恫ćÆ悂ćØ悂ćØglobalćŒć‚ć‚Šć€ć“ć‚ŒćŒć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®å½¹å‰²ć‚’ęžœćŸć—ć¦ć„ć¾ć—ćŸć€‚

node.jsć«ćŠć‘ć‚‹ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈
foo = 123;

console.log(global.foo); // 123

ć¾ć node恫globalThisćÆå®Ÿč£…ć•ć‚Œć¦ć„ć¾ć›ć‚“ćŒć€globalThisć‚’å®Ÿč£…ć™ć‚‹PR悒見悋恫态悄ćÆ悊态ćØć„ć†ć‹å½“ē„¶ćŖ恌悉态global === globalThisćØćŖ悋悈恆恧恙怂

thisć‹ć‚‰ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć‚’å–å¾—ć™ć‚‹ę–¹ę³•

ć•ć¦ć€ć“ć“ć¾ć§ć§čؘäŗ‹ć‚æć‚¤ćƒˆćƒ«ć®ć†ć”globalThisćØglobal恌å‡ŗć¦ćć¾ć—ćŸćŒć€thisćÆä½•ć®é–¢äæ‚ćŒć‚ć‚‹ć®ć§ć—ć‚‡ć†ć‹ć€‚

恓悌悂JavaScriptć«é–¢ć—ć¦ć‚ˆćēŸ„悉悌恟äŗ‹å®Ÿć§ć™ćŒć€ćƒˆćƒƒćƒ—ćƒ¬ćƒ™ćƒ«ć®ļ¼ˆé–¢ę•°ć®äø­ć§ćŖ恄ļ¼‰ć‚³ćƒ¼ćƒ‰ć‚„ć€é–¢ę•°ć‚’ę™®é€šć«ļ¼ˆć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®ćƒ”ć‚½ćƒƒćƒ‰ćØ恗恦恧ćÆćŖ恏ļ¼‰å‘¼ć‚“ć å “合ćÆthisćŒć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恫ćŖ悋恓ćØ恌ēŸ„ć‚‰ć‚Œć¦ć„ć¾ć™ć€‚

thisćŒć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恫ćŖć‚‹ä¾‹
foo = 123;
console.log(this.foo); // thisćŒć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćŖ恮恧this.fooćÆ123
showFoo();

function showFoo() {
  // é–¢ę•°ć®äø­ć§ć‚‚thisćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈
  console.log(this.foo);
}

恟恠恗态strictćƒ¢ćƒ¼ćƒ‰ć®å “åˆćÆé–¢ę•°ć®äø­ć§ćÆthisćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恧ćÆćŖ恏undefined恫ćŖć‚‹ć®ć§ć“ć®ę–¹ę³•ćÆä½æćˆć¾ć›ć‚“ć€‚

foo = 123;
showFoo();

function showFoo() {
  'use strict';
  // this恌undefinedćŖć®ć§ć“ć‚ŒćÆć‚Øćƒ©ćƒ¼
  console.log(this.foo);
}

ćć®äø€ę–¹ć€ć‚°ćƒ­ćƒ¼ćƒćƒ«ć®ļ¼ˆé–¢ę•°ć®äø­ć§ćÆćŖ恄ļ¼‰thisćÆstrictćƒ¢ćƒ¼ćƒ‰ć§ć‚‚ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćØć—ć¦åˆ©ē”ØåÆčƒ½ć§ć™ć€‚

<script>
  'use strict';
  var foo = 123;
  // ćƒˆćƒƒćƒ—ćƒ¬ćƒ™ćƒ«ć®thisćÆstrictćƒ¢ćƒ¼ćƒ‰ć§ć‚‚ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈
  // ćØć—ć¦åˆ©ē”ØåÆčƒ½ćŖ恮恧123恌č”Øē¤ŗ恕悌悋
  console.log(this.foo); 
  showFoo();

  function showFoo() {
    // é–¢ę•°ć®äø­ć§ćÆthisćÆundefinedćŖ恮恧ć‚Øćƒ©ćƒ¼
    console.log(this.foo);
  }
</script>

ćŸć č£ęŠ€ćŒć‚ć£ć¦ć€Functionć‚³ćƒ³ć‚¹ćƒˆćƒ©ć‚Æć‚æ悒ä½æ恆恓ćØ恧strictćƒ¢ćƒ¼ćƒ‰ć®ć‚³ćƒ³ćƒ†ć‚­ć‚¹ćƒˆå†…ć§ć‚‚strictćƒ¢ćƒ¼ćƒ‰ć§ćŖć„é–¢ę•°ć‚’ä½œć‚‹ć“ćØćŒć§ćć¾ć™ć€‚

<script>
  'use strict';
  var globalObj = (new Function('return this;'))();
  console.log(globalObj === globalThis); // true
</script>

恗恋恗恝恆ćÆć„ć£ć¦ć‚‚ć€ć“ć®č£ęŠ€ćŒć†ć¾ćå‹•ć‹ćŖ恄ē’°å¢ƒćŒå°‘ę•°å­˜åœØć—ć¾ć™ć€‚å…·ä½“ēš„恫ćÆChrome Apps恧ä½æ恈ćŖ恄恝恆恧恙3怂恂ćØ動ēš„ć«é–¢ę•°ć‚’ä½œć‚‹ć®ćŒę°—ęŒć”ę‚Ŗ恄恧恙恭怂

ć¾ćØ悁悋ćØć€ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć«ć‚¢ć‚Æć‚»ć‚¹ć™ć‚‹ę—¢å­˜ć®ę–¹ę³•ćÆäø€é•·äø€ēŸ­ć§ć€å®Œē’§ćŖ悂恮ćÆć‚ć‚Šć¾ć›ć‚“ć§ć—ćŸć€‚

  • thisćÆstrictćƒ¢ćƒ¼ćƒ‰ć®é–¢ę•°å†…ć§ćÆä½æē”ØäøåÆć€‚ć¾ćŸć€å®ŸćÆnode.js恧ćÆthisćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恧ćÆćŖ恄怂
  • (new Function('return this'))()ćÆChrome Apps恧ä½æē”ØäøåÆ怂
  • windowćÆćƒ–ćƒ©ć‚¦ć‚¶ē’°å¢ƒć®ćæ怂
  • globalćÆnode.js恮ćæ怂

ćć—ć¦globalThis

äøŠć®ćƒŖć‚¹ćƒˆć®äøŠ2恤ćÆęؙęŗ–ć§å®šć‚ć‚‰ć‚ŒćŸę–¹ę³•ć§ć™ćŒä½æ恈悋ēŠ¶ę³ćŒé™ć‚‰ć‚Œć¾ć™ć—态äø‹ć®2恤ćÆECMAScriptęؙęŗ–ć«å«ć¾ć‚Œć¦ć„ćŖ恄恟悁ćÆē’°å¢ƒć«ć‚ˆć£ć¦ä½æćˆć‚‹ć‚‚ć®ćŒē•°ćŖ悊ēµ±äø€ć•ć‚Œć¦ć„ćŖ恄ćØć„ć†ć®ćŒē¾ēŠ¶ć§ć—ćŸć€‚ćć®äø€ę–¹ć§ć€ä»Šć®ę™‚ä»£ć€å…Ø恦恮ē’°å¢ƒć§å…±é€šć®JavaScriptć‚³ćƒ¼ćƒ‰ć‚’å‹•ć‹ć—ćŸć„å “é¢ćÆć‚ˆćć‚ć‚Šć¾ć™ć€‚ļ¼ˆć“恮ꦂåæµćÆę™‚ä»£ć«ć‚ˆć£ć¦å‘¼ć°ć‚Œę–¹ćŒć„ć‚ć„ć‚ćØåˆ†ć‹ć‚Œć¦ć„ć‚‹ć‚ˆć†ćŖę°—ćŒć—ć¾ć™ćŒć€č‡Ŗ分ćÆIsomorphic JavaScriptćØć‹å‘¼ć¶ć®ćŒć—ć£ćć‚Šę„ć¾ć™ć€‚ļ¼‰

恝恓恧态ECMAScriptęؙęŗ–ćØ恗恦ēµ±äø€ć•ć‚ŒćŸę–¹ę³•ć‚’ē”Øꄏ恗悈恆ćØ恄恆恓ćØ恧ē™»å “ć—ćŸć®ćŒglobalThisćŖ恮恧恙怂

globalThis恮ꦂåæµč‡Ŗ体ćÆč¦ć™ć‚‹ć«ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćŖ恮恧ē°”å˜ć§ć™ć­ć€‚ćŸć ć€ć“ć‚Œć‚’čŖ­ć‚“恧恄悋恂ćŖ恟ćÆ态šŸ‘»globalThisšŸ‘»ćØć„ć†åå‰ćÆ恩恆ćŖ恮ćØę€ć£ćŸć®ć§ćÆćŖ恄恧恗悇恆恋怂ē§ćÆę€ć„ć¾ć—ćŸć€‚JavaScriptćŒå«Œć„ćŖäŗŗćŸć”ć«ć‚ˆć‚‹ćƒć‚¬ćƒ†ć‚£ćƒ–ć‚­ćƒ£ćƒ³ćƒšćƒ¼ćƒ³ć®åÆ¾č±”ć«ćŖć£ć¦ć„ć‚‹ć®ć‚’ć‚ˆćč¦‹ć‚‹this悒å…Øé¢ć«ęŠ¼ć—å‡ŗć—ć¦ćć‚‹ć®ćÆćŖ恋ćŖć‹ę”»ć‚ćŸćƒćƒ¼ćƒŸćƒ³ć‚°ć§ć™ć€‚ćć‚‚ćć‚‚ć€globalThisćØć„ć†åå‰ć‚’č¦‹ć¦ć‚‚ä½•ćŒčØ€ć„ćŸć„ć®ć‹ć‚ˆćåˆ†ć‹ć‚Šć¾ć›ć‚“ć€‚

ć¾ć‚ć€ćØćÆć„ćˆć€ę°—ęŒć”ćÆåˆ†ć‹ć‚‰ćŖć„ć§ć‚‚ć‚ć‚Šć¾ć›ć‚“ć€‚thisćÆćƒˆćƒƒćƒ—ćƒ¬ćƒ™ćƒ«ć®ļ¼ˆé–¢ę•°å†…恧ćŖ恄ļ¼‰ē’°å¢ƒć§ćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć§ć™ć‹ć‚‰ć€ć€Œćć®ć‚ˆć†ćŖļ¼ˆć‚°ćƒ­ćƒ¼ćƒćƒ«ćŖļ¼‰ē’°å¢ƒć§ć®thisć‚’å‚ē…§ć§ćć‚‹ć‚‚ć®ć€ćØ恄恆恓ćØ恧globalThisćØć„ć†å‘½åćŖ恮恧恗悇恆怂


ćŸć ć€ć“ć“ć§ć²ćØ恤čØ€ćˆć‚‹ć“ćØćÆ态globalThisćÆ実ćÆę¬”å–„ē­–恠ćØ恄恆恓ćØ恧恙怂悂ćØ悂ćØćÆć€ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈悒č”Øć™å¤‰ę•°ć®åå‰ćÆglobal恫恙悋äŗˆå®šć§ć—ćŸć€‚ć¤ć¾ć‚Šć€ę—¢ć«å­˜åœØć—ć¦ć„ćŸnode.jsć®ä»•ę§˜ć«åˆć‚ć›ć‚ˆć†ćØć—ćŸć®ć§ć™ć€‚

ćØć“ć‚ćŒć€å®Ÿéš›ć«globalć‚’å®Ÿč£…ć—ć¦ćæ悋ćØć€å‹•ć‹ćŖ恏ćŖć‚‹ć‚¦ć‚§ćƒ–ć‚µć‚¤ćƒˆćŒć„ćć¤ć‹ē™ŗē”Ÿć—ć¦ć—ć¾ć„ć¾ć—ćŸć€‚ä¾‹ćˆć°ć“ć®ć‚¹ćƒ¬ćƒƒćƒ‰ć§ćÆ态globalćŒå­˜åœØ恙悌恰node.jsē’°å¢ƒć§ć‚ć‚‹ćØåˆ¤ę–­ć—ć¦node.jså‘ć‘ć®å‡¦ē†ć‚’č”Œć†ć‚ˆć†ćŖć‚³ćƒ¼ćƒ‰ćŒå­˜åœØ恙悋恓ćØ恌꘎悉恋ćØćŖć‚Šć€ćć‚ŒćŒåŽŸå› ć§ć‚¦ć‚§ćƒ–ć‚µć‚¤ćƒˆćŒå‹•ä½œć—ćŖ恏ćŖć£ćŸäŗ‹ä¾‹ćŒå ±å‘Šć•ć‚Œć¦ć„ć¾ć™ć€‚
ć”ć‚‡ć£ćØęœ¬é”Œć‹ć‚‰ćšć‚Œć¾ć™ćŒć€ć¾ć£ćŸććµć–ć‘ćŸć‚³ćƒ¼ćƒ‰ć‚’ę›ø恏äŗŗć‚‚ć„ćŸć‚‚ć®ć§ć™ć­ć€‚ć“ć‚ŒćÆglobalć‚’ć€Œć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈怍ćØ恗恦ä½æ恆恮恧ćÆćŖ恏怌node.jsć‹ć©ć†ć‹åˆ¤å®šć™ć‚‹ćŸć‚ć®ćƒ•ćƒ©ć‚°ć€ćØ恗恦ä½æē”Øć™ć‚‹ć‚³ćƒ¼ćƒ‰ć‚’ę›øć„ćŸćŸć‚ć«čµ·ć“ć£ćŸå•é”Œć§ć™ć€‚global恧ćÆćŖ恏process悒ä½æē”Øć™ć‚‹ć‚³ćƒ¼ćƒ‰ćŖ恩悂ē›®ć«ć—ć¾ć™ćŒå°†ę„ćŒå±ć¶ć¾ć‚Œć¾ć™ć­ć€‚

č©±ć‚’ęˆ»ć™ćØ态JavaScriptćÆå¾Œę–¹äŗ’ę›ę€§ćŒé‡č¦ćŖč؀čŖžć§ć™ć€‚ć§ć™ć‹ć‚‰ć€å®Ÿéš›ć«ć‚¦ć‚§ćƒ–ć‚µć‚¤ćƒˆćŒå£Šć‚Œć¦ć—ć¾ć†äŗ‹ä¾‹ćŒć„ćć¤ć‚‚ē™ŗē”Ÿć—ćŸć“ćØ恧global恮ꎔē”ØćÆé “ęŒ«ć—ć€åˆ„ć®åå‰ć‚’ęŽ¢ć•ćŖ恑悌恰恄恑ćŖ恏ćŖć‚Šć¾ć—ćŸć€‚ę–°ć—ć„åå‰ć«é–¢ć™ć‚‹ć‚ć”ć‚ƒć‚ć”ć‚ƒć—ćŸč­°č«–ć‚’GitHub恧見悋恓ćØćŒć§ćć¾ć™ć€‚

ć“ć‚Œć®ćŠć‹ć’ć§global恮ęؙęŗ–化ćÆē“„2å¹“ć‚‚é…ć‚ŒćŸć‚ć‘ć§ć™ćŒć€ä½•ć ć‹ć‚“ć ć§globalThisćØć„ć†åå‰ć«ę±ŗć¾ć‚Šć€Chrome 71恧恤恄恫ꗄ恮ē›®ć‚’見悋恓ćØ恫ćŖć£ćŸć‚ć‘ć§ć™ć€‚globalThisćØć„ć†åå‰ć«ć¤ć„ć¦ćÆćƒ—ćƒ­ćƒćƒ¼ć‚¶ćƒ«ć«å°‘ć—čŖ¬ę˜ŽćŒć‚ć‚Šć¾ć™ć€‚

After some data-gathering to determine web compatibility of a short list of choices, we settled on globalThis, as this name is both highly likely to be web compatible, and also maps to the concept of being the ā€œglobal this valueā€ (and not the ā€œglobal objectā€, per above).

ć¾ć‚č¦ć™ć‚‹ć«å…ˆć»ć©čæ°ć¹ćŸć‚ˆć†ćŖć€ć‚°ćƒ­ćƒ¼ćƒćƒ«ćŖthisć®å€¤ć ć‹ć‚‰globalThisćŖ悓恠悈ćØ恄恆恓ćØ恌ę›øć„ć¦ć‚ć‚Šć¾ć™ć­ć€‚

ć¾ćØ悁

ć“ć“ć¾ć§čæ°ć¹ćŸć“ćØćÆ恠恄恟恄globalThisć®ćƒ—ćƒ­ćƒćƒ¼ć‚¶ćƒ«ć«ę›øć„ć¦ć‚ć‚Šć¾ć™ć€‚ę—¢ć«ć“ć‚Œć‚’čŖ­ć‚“ć§ć„ćŸę–¹ć«ćØć£ć¦ćÆę–°é®®å‘³ćŒćŖć„å†…å®¹ć ć£ćŸć‹ć‚‚ć—ć‚Œć¾ć›ć‚“ćŒć€ć¾ć‚ćć®ć‚ˆć†ćŖę–¹ćÆ1č”Œē›®ć§ę—¢ć«åÆŸć—ćŸćØę€ć†ć®ć§å¤§äøˆå¤«ć§ć—悇恆怂

č¦ć™ć‚‹ć«ć€ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć‚’ę‰±ć†ę—¢å­˜ć®ę–¹ę³•ćÆć©ć‚Œć‚‚äø€é•·äø€ēŸ­ć§ēµ±äø€ć•ć‚ŒćŸę–¹ę³•ćŒē„”ć‹ć£ćŸć®ć§ć€å…Ø恦恮ē’°å¢ƒć§ä½æ恈悋globalThisć‚’ä½œć‚Šć¾ć—ćŸćØ恄恆恓ćØ恧恙怂ē¾åœØ恮ćØć“ć‚ć“ć‚ŒćŒåˆ©ē”ØåÆčƒ½ćŖ恮ćÆGoogle Chrome恮ćæć§ć™ćŒć€ä»–ć®ćƒ–ćƒ©ć‚¦ć‚¶ć‚„node.js悂čæ‘恄恆恔恫čæ½éšć™ć‚‹ć®ć§ćÆćŖ恄恋ćØę€ć„ć¾ć™ć€‚

ćØćÆć„ćˆć€å®Ÿå‹™äøŠćÆå¤ć„ē’°å¢ƒć‚‚č€ƒę…®ć—ćŖ恑悌恰ćŖ悉ćŖ恄恮恧globalThis恠恑ä½æćˆć°ä»–ćÆ要悉ćŖ恄态ćØć„ć†ć‚ć‘ć«ćÆ恄恋ćŖć„ć®ćŒć¤ć‚‰ć„ćØ恓悍恧恙怂悀恗悍globalThisåƾåæœćŒåŠ ć‚ć£ćŸēµęžœć‚³ćƒ¼ćƒ‰ćŒč¤‡é›‘åŒ–ć™ć‚‹ć‹ć‚‚ć—ć‚Œć¾ć›ć‚“ć€‚

ēš†ć•ć‚“ć‚‚ć‚‚ć—ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć‚’ę‰±ć†ę©Ÿä¼šćŒć‚ć‚Œć°ć€globalThis恮恓ćØ悒ꀝ恄å‡ŗć—ć¦ć‚ć’ć¦ćć ć•ć„ć€‚

ä½™č«‡

今回ćÆä½™č«‡ć‚’ć¾ćØć‚ć®å¾Œć«ęŒć£ć¦ćć¦ćæć¾ć—ćŸć€‚ä»„é™ć€ä»•ę§˜ę›øćØć„ć†å “åˆćÆECMAScriptĀ® 2018 Language Specificationć‚’ęŒ‡ć™ć‚‚ć®ćØć—ć¾ć™ć€‚ļ¼ˆglobalThisćÆES2018恫ćÆå…„ć£ć¦ć„ć¾ć›ć‚“ćŒć¾ć‚å¤§äøˆå¤«ć§ć™ć€‚ļ¼‰

ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚·ć‚¹ćƒ†ćƒ ćØć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈

ćØć“ć‚ć§ć€ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć£ć¦é›£ć—ć„ę¦‚åæµć§ć™ć‚ˆć­ć€‚ćƒ–ćƒ©ć‚¦ć‚¶äøŠć®JavaScript恧ćÆć€ä½•ć‚‚å®£čØ€ć›ćšć«ć„ććŖć‚Šä»£å…„ć•ć‚ŒćŸå¤‰ę•°ć‚„ć€é–¢ę•°ć®å¤–ć§varć§å®£čØ€ć•ć‚ŒćŸå¤‰ę•°ćŒć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ćØćŖć‚Šć¾ć™ć€‚å‰č€…ćÆstrictćƒ¢ćƒ¼ćƒ‰ć§ćÆä½æćˆć¾ć›ć‚“ćŒć€‚

äø‹ć®ć‚³ćƒ¼ćƒ‰ć§ē¢ŗ恋悁恦ćæ悋ćØ态varć§å®£čØ€ć—ćŸå¤‰ę•°fooćŒć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®ćƒ—ćƒ­ćƒ‘ćƒ†ć‚£ćØ恗恦å‡ŗē¾ć—恦恄悋恓ćØćŒåˆ†ć‹ć‚Šć¾ć™ć­ć€‚

<script>
  'use strict';
  var foo = 123;
  console.log(globalThis.foo); // 123
</script>

ā€¦ā€¦ć€‚

恓恓恧态怌恄悄态違恆恧恗悇怍ćØę€ć£ćŸäŗŗćÆ恋ćŖ悊JavaScriptć«č©³ć—ć„ć§ć™ć­ć€‚ć€Œå½“ćŸć‚Šå‰ć®ć“ćØć‚’ä½•ć‚’ä»Šć•ć‚‰ć€ćØę€ć£ćŸäŗŗćÆäæ®č”Œäøč¶³ć§ć™ć€‚

ćć†ć€ć•ć£ćć®čŖ¬ę˜Žć«ćÆ実ćÆčŖ¤ć£ć¦ć„ć‚‹ćØć“ć‚ćŒć‚ć‚Šć¾ć™ć€‚ć©ć“ćŒé•ć†ć®ć‹ćŠåˆ†ć‹ć‚Šć§ć—ć‚‡ć†ć‹ć€‚

ē­”ćˆćÆ仄äø‹ć®ć‚³ćƒ¼ćƒ‰ć§ć™ć€‚

<script type="module">
  'use strict';
  var foo = 123;
  console.log(globalThis.foo); // undefined
</script>

ćŖ悓ćØ态script要ē“ ć«type="module"å±žę€§ć‚’ä»˜ć‘ćŸć‚‰ēµęžœćŒå¤‰ć‚ć‚Šć¾ć—ćŸć€‚ćƒ–ćƒ©ć‚¦ć‚¶äøŠć§ć®JavaScript悒ēŸ„ć£ć¦ć„ć‚‹ę–¹ćÆ态type="module"ćØć„ć†ć®ćŒimportꖇ悒ä½æ恆ćØćć«åæ…要ćŖ悂恮恧恂悋恓ćØćÆćŠåˆ†ć‹ć‚Šć ćØę€ć„ć¾ć™ćŒć€ECMAScriptēš„恫ćÆ恓悌ćÆ内éƒØć®ć‚³ćƒ¼ćƒ‰ć‚’Script恧ćÆćŖ恏ModulećØć—ć¦č©•ä¾”ć™ć‚‹ć‚ˆć†ć«ć™ć‚‹åŠ¹ęžœćŒć‚ć‚Šć¾ć™ć€‚å®ŸćÆECMAScript恧ćÆćƒ—ćƒ­ć‚°ćƒ©ćƒ ćÆScriptćØModule恮2ēØ®é”žć«åˆ†é”žć•ć‚Œć¦ćŠć‚Šć€exportꖇ悄importꖇćÆModule恮äø­ć§ć—恋ä½æ恈ćŖ恄꧋ꖇ恧恙怂ꙮ通恮script要ē“ ć§čŖ­ćæć“ć¾ć‚Œć‚‹ć‚ˆć†ćŖå¾“ę„ć®JavaScriptćÆScript恧恙怂

ćć—ć¦ć€ćƒˆćƒƒćƒ—ćƒ¬ćƒ™ćƒ«ć®varćÆScript恧ćÆć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć‚’ä½œć‚‹äø€ę–¹ć€Module恧ćÆ恝恆恧ćÆćŖ恄恮恧恙怂ćŖ恊态Module恮äø­ć§ćÆč‡Ŗ動ēš„恫strictćƒ¢ćƒ¼ćƒ‰ć«ćŖ悋恟悁态ē„”宣čØ€ć§å¤‰ę•°ć«ä»£å…„ć—ć¦ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć‚’ä½œć‚‹ć“ćØćÆć§ćć¾ć›ć‚“ć€‚ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć‚’ä½œć‚ŠćŸć‘ć‚Œć°ć€ę˜Žē¤ŗēš„ć«ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć‚’ä»‹ć™ć‚‹åæ…č¦ćŒć‚ć‚Šć¾ć™ć€‚

ć“ć®ęŒ™å‹•ćÆć‚ˆćć‚ˆćč€ƒćˆć¦ćæć‚Œć°å½“ē„¶ć§ć™ć­ć€‚ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć«åˆ†ć‘ć‚‰ć‚ŒćŸćƒ—ćƒ­ć‚°ćƒ©ćƒ ć«ćŠć„ć¦ć€å„ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ćÆē‹¬ē«‹ć—恦恄悋ļ¼ˆē‹¬ē«‹ć—ćŸå¤‰ę•°ć‚¹ć‚³ćƒ¼ćƒ—ć‚’ęŒć£ć¦ć„ć‚‹ļ¼‰ć¹ćć§ć™ć€‚ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«å†…éƒØ恧varć§ä½œć£ćŸå¤‰ę•°ćŒć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć«ćŖć£ć¦ę°—č»½ć«ä»–ć«å½±éŸæ悒äøŽćˆć¦ć‚‚ć‚‰ć£ć¦ćÆå›°ć‚Šć¾ć™ć€‚

čØ€č‘‰ć ć‘ć ćØåˆ†ć‹ć‚Šć«ćć„ć‹ć‚‚ć—ć‚Œć¾ć›ć‚“ć®ć§ć€ä¾‹ć§čŖ¬ę˜Žć—ć¾ć™ć€‚index.htmlćØmod.js恋悉ćŖć‚‹ćƒ—ćƒ­ć‚°ćƒ©ćƒ ć‚’č€ƒćˆć¾ć—ć‚‡ć†ć€‚

index.html
<script type="module">
  import { setFoo } from './mod.js';

  var foo = 0;
  setFoo(123456);
  console.log(foo);
</script>
mod.js
var foo;

export function setFoo(value) {
  foo = value;
}

ć“ć®ä¾‹ć§ć€index.html恮console.log恧č”Øē¤ŗć•ć‚Œć‚‹å€¤ćÆćŖ悓恧恗悇恆恋怂

ē­”ćˆćÆ0恧恙怂123456恧ćÆć‚ć‚Šć¾ć›ć‚“ć€‚ć“ć‚ŒćÆć¾ć•ć«ć€mod.jså†…ć§å®šē¾©ć•ć‚ŒćŸå¤‰ę•°fooćŒć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć§ćÆćŖ恄ļ¼ˆćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć®ć‚¹ć‚³ćƒ¼ćƒ—å†…ć®å¤‰ę•°ć«ćŖć£ć¦ć„ć‚‹ļ¼‰ć“ćØ恮čØ¼å·¦ć§ć‚ć‚‹ć“ćØćŒćŠåˆ†ć‹ć‚Šć«ćŖ悋恋ćØę€ć„ć¾ć™ć€‚

ć“ć®ć‚ˆć†ć«ć€ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć«åˆ†ć‘ć‚‰ć‚ŒćŸJavaScript恧ćÆćƒ¢ć‚øćƒ„ćƒ¼ćƒ«é–“ć§å¤‰ę•°ć®ć‚¹ć‚³ćƒ¼ćƒ—ć‚’åˆ†ć‘ć‚‹ć“ćØć§ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«é–“ć®ē‹¬ē«‹ę€§ć‚’é«˜ć‚ć¦ć„ć¾ć™ć€‚

ćØć“ć‚ć§ć€ę¬”ć®å®ŸéؓćØ恗恦mod.jsć‚’å¤‰ćˆć¦ę¬”ć®ć‚ˆć†ć«ć—ć¦ćæć¾ć—ć‚‡ć†ć€‚

index.html
<script type="module">
  import { setFoo } from './mod.js';

  var foo = 0;
  setFoo(123456);
  console.log(foo);
</script>
mod.js
export function setFoo(value) {
  globalThis.foo = 123456;
}

ć“ć®å “åˆćÆconsole.logć§ä½•ćŒč”Øē¤ŗ恕悌悋恧恗悇恆恋怂

ē­”ćˆćÆć‚„ć£ć±ć‚Š0恧恙怂怌123456恠悍怍ćØę€ć£ćŸę–¹ćÆć”ć‚‡ć£ćØå‰ć‹ć‚‰ć‚‚ć†äø€åŗ¦čŖ­ćæćŖ恊恙ćØć„ć„ć‹ć‚‚ć—ć‚Œć¾ć›ć‚“ć€‚

ä»•ę§˜ę›ø恧ē¢ŗ恋悁悋

今čŖ¬ę˜Žć—ćŸč«øć€…ć‚’ä»•ę§˜ę›ø恧ē¢ŗ恋悁恦ćæ恟恄ćØć„ć†ę–¹å‘ć‘ć®čŖ¬ę˜Žć‚‚ē”Øę„ć—ć¾ć—ćŸć€‚ć¾ćšScriptć«ćŠć‘ć‚‹ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć®åˆęœŸåŒ–ćÆ15.1.11 Runtime Semantics: GlobalDeclarationInstantiationć§č”ŒćŖć‚ć‚Œć¾ć™ć€‚ć‚¹ćƒ†ćƒƒćƒ—7恧VarScopedDeclarationsć‚’ć€ć‚¹ćƒ†ćƒƒćƒ—15恧LexicallyScopedDeclarations悒č؈ē®—ć—ć¦ć„ć¾ć™ćŒć€å‰č€…ćŒvarć§å®£čØ€ć•ć‚ŒćŸå¤‰ę•°ćŸć”ć€å¾Œč€…ćŒlet悄constć§å®£čØ€ć•ć‚ŒćŸå¤‰ę•°ćŸć”ć«ē›øå½“ć—ć¾ć™ć€‚å¾Œč€…ć®å‡¦ē†ćÆć‚¹ćƒ†ćƒƒćƒ—16ć§č”ŒćŖć‚ć‚Œć¦ćŠć‚Šć€ęµć—čŖ­ćæ恙悋ćØenvRec.CreateMutableBindingćŒå‘¼ć°ć‚Œć¦ć„ć¾ć™ć€‚ć“ć‚ŒćÆ要恙悋恫态ē¾åœØć®ć‚¹ć‚³ćƒ¼ćƒ—ć«å¤‰ę•°ć‚’ä½œęˆć™ć‚‹ćØ恄恆恓ćØć§ć™ć€‚ć“ć®ć€Œē¾åœØć®ć‚¹ć‚³ćƒ¼ćƒ—ć€ćÆ実ćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚¹ć‚³ćƒ¼ćƒ—ļ¼ˆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®ćƒ—ćƒ­ćƒ‘ćƒ†ć‚£ćØ恗恦ē¾ć‚Œć‚‹ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ćŸć”ć®ć‚¹ć‚³ćƒ¼ę—„ļ¼‰ć§ćÆćŖ恄ćØ恄恆恓ćØćŒćƒć‚¤ćƒ³ćƒˆć§ć™ć€‚

äø€ę–¹ć€varć§å®£čØ€ć•ć‚ŒćŸå¤‰ę•°ćŸć”ć®å‡¦ē†ćÆć‚¹ćƒ†ćƒƒćƒ—18ć§č”ŒćŖć‚ć‚Œć¦ć„ć¾ć™ć€‚ć“ć”ć‚‰ćÆenvRec.CreateGlobalVarBindingć§å‡¦ē†ć•ć‚Œć¦ćŠć‚Šå…ˆć»ć©ćØćÆé•ć„ć¾ć™ć­ć€‚ć“ć‚Œć‚’ä½æć£ćŸå “åˆćÆć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ļ¼ˆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®ćƒ—ćƒ­ćƒ‘ćƒ†ć‚£ļ¼‰ćØ恗恦ē™»éŒ²ć•ć‚Œć¾ć™ć€‚ć“ć“ć¾ć§ć‚’ć¾ćØć‚ć¦ä¾‹ć§ē¢ŗ恋悁恦ćæć¾ć—ć‚‡ć†ć€‚

<script>
// å®Ÿč”Œé–‹å§‹ę™‚ē‚¹ć§äøŠčØ˜ć®GlobalDeclarationInstatiationćÆ
// č”ŒćŖć‚ć‚Œć¦ć„ć‚‹ć®ć§ć€ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恫äø‹ć§å®£čØ€ć•ć‚Œć¦ć„ć‚‹å¤‰ę•°fooćŒå­˜åœØ
console.log('foo' in globalThis); // true

// var宣čØ€ćŒå®Ÿéš›ć«å®Ÿč”Œć•ć‚Œć‚‹ć¾ć§å€¤ćÆundefined
console.log(globalThis.foo); // undefined

var foo = 123;
// fooć«ä»£å…„ć•ć‚ŒćŸć®ć§globalThis.fooć‚‚å½“ē„¶ę›ø恍ꏛ悏悋
console.log(globalThis.foo); // 123

// globalThis.fooćøć®ä»£å…„ćÆåę˜ ć•ć‚Œć‚‹
globalThis.foo = 99999;
console.log(foo); // 99999

let bar = 345;
// letć§å®£čØ€ć—ćŸå¤‰ę•°ćÆć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć§ćÆćŖ恄
console.log('bar' in globalThis); // false

// ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć®bar悒ę›øćę›ćˆć¦ć‚‚ć“ć®å¤‰ę•°bar恫ćÆå½±éŸæ恗ćŖ恄
globalThis.bar = 99999;
console.log(bar); // 345

// é€†ć‚‚åŒę§˜
bar = -5;
console.log(globalThis.bar); // 99999
</script>

äø€ę–¹ć§Moduleć®å®Ÿč”Œę™‚ćÆä½•ćŒčµ·ć“ć£ć¦ć„ć‚‹ć§ć—ć‚‡ć†ć‹ć€‚ćć‚Œć‚’ē¢ŗ恋悁悋恫ćÆ15.2.1.16.4 Instantiate() Concrete Methodć‹ć‚‰å‚ē…§ć•ć‚Œć¦ć„ć‚‹15.2.1.16.4.2 ModuleDeclarationEnvironmentSetupć‚’č¦‹ć¾ć™ć€‚ć•ć£ćć®GlobalDeclarationInstantiationćØć”ć‚‡ć£ćØä¼¼ćŸć‚ˆć†ćŖ恓ćØ恌ę›øć„ć¦ć‚ć‚Šć¾ć™ć­ć€‚

恟恠恗态varć§å®£čØ€ć•ć‚ŒćŸå¤‰ę•°ćŸć”ć‚’å‡¦ē†ć™ć‚‹éƒØåˆ†ć§ć‚ć‚‹ć‚¹ćƒ†ćƒƒćƒ—12悒見悋ćØ态CreateGlobalVarBinding恧ćÆćŖ恏CreateMutableBinding恌ä½æć‚ć‚Œć¦ć„ć¾ć™ć­ć€‚ć“ć‚ŒćŒå…ˆć»ć©ć®Scriptć®å “åˆćØ恮違恄恧恂悊态Moduleć®å “åˆć«varć§å®£čØ€ć—ćŸå¤‰ę•°ćŒć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ćØćÆćŖ悉ćŖ恄ļ¼ˆćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć®ćƒˆćƒƒćƒ—ćƒ¬ćƒ™ćƒ«ć‚¹ć‚³ćƒ¼ćƒ—ć®å¤‰ę•°ćØćŖ悋ļ¼‰ć“ćØ恮čØ¼å·¦ć§ć™ć€‚

<script type="module">
// å…ˆć»ć©ćØćÆē•°ćŖ悊态varć§å®£čØ€ć—ćŸå¤‰ę•°ćŒć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć«å­˜åœØ恗ćŖ恄
var foo = 123;
console.log('foo' in globalThis); // false

// globalThis.fooć«ä½•ć‹ć‚’ä»£å…„ć—ć¦ć‚‚ć“ć®å¤‰ę•°foo恫ćÆå½±éŸæ恗ćŖ恄
globalThis.foo = 99999;
console.log(foo); // 123
</script>

ć“ć®ć‚ˆć†ć«ć€Module恧ćÆvarć§å®£čØ€ć—ćŸå¤‰ę•°ćŒć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ćØćŖć‚Šć¾ć›ć‚“ć€‚ć“ć®ć“ćØćÆę³Øę„ć—ć¦ć„ćŖ恄ćØē½ ć«ćÆć¾ć‚‹ć“ćØćŒć‚ć‚‹ć‹ć‚‚ć—ć‚Œć¾ć›ć‚“ć€‚

node.jsć®ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚·ć‚¹ćƒ†ćƒ 

恧ćÆć€ä»•ę§˜ę›ø恋悉ćÆé›¢ć‚Œć¦ę¬”ć®č©±é”Œć«ē§»ć‚Šć¾ć—悇恆怂JavaScript悒恰悊恰悊ę›øćę–¹ć®äø­ć§ć‚‚ć€å‰ēÆ€ć§å–ć‚ŠäøŠć’ćŸES Modulesļ¼ˆimportćØ恋exportćØ恋悒ä½æć†ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚·ć‚¹ćƒ†ćƒ ļ¼‰ć«ćÆć‚ć¾ć‚Šé¦“ęŸ“ćæ恌ćŖ恄ćØć„ć†ę–¹ćŒć„ć‚‹ć‹ć‚‚ć—ć‚Œć¾ć›ć‚“ć€‚node.jsē”Ø恮JavaScriptćƒ—ćƒ­ć‚°ćƒ©ćƒ ć«ćŠć„ć¦ćÆć¾ć ć¾ć CommonJS Modules恌ē¾å½¹ć§ć™ć€‚require悒ä½æć£ć¦ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’čŖ­ćæč¾¼ć‚€ć‚„ć¤ć§ć™ć­ć€‚node.js悂ES Modules恮åƾåæœć‚’é€²ć‚ć¦ć„ć¾ć™ćŒć€v10ć§ć‚„ć£ćØ試éØ“å®Ÿč£…ćŒåˆ©ē”ØåÆčƒ½ć«ćŖć£ćŸę®µéšŽć§ć™ć€‚

ćć—ć¦ć€node.jsć«ćŠć‘ć‚‹å„ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ćÆ态Script恋Moduleć‹ć®åˆ†é”žć§ć„ćˆć°ScriptćØćŖć‚Šć¾ć™ć€‚ć˜ć‚ƒć‚ć€ćƒˆćƒƒćƒ—ćƒ¬ćƒ™ćƒ«varć§å¤‰ę•°ć‚’ä½œć‚‹ćØ恩恆ćŖć‚‹ć§ć—ć‚‡ć†ć‹ć€‚ć•ć£ććč©¦ć—ć¦ćæć¾ć™ć€‚ļ¼ˆå‹•ä½œć‚’ē¢ŗčŖć§ćć‚‹ć‚ˆć†ć«globalThisć®ä»£ć‚ć‚Šć«global悒ä½æē”Øć—ć¦ć„ć¾ć™ć€‚ļ¼‰

node.js
console.log('foo' in global); // false

var foo = 123;
console.log(global.foo); // undefined

ā€¦ā€¦ļ¼Ÿ

ćŖć‚“ć‹ć€å…ˆć»ć©ć®čŖ¬ę˜ŽćØé•ć„ć¾ć™ć­ć€‚varć§å®£čØ€ć—ćŸå¤‰ę•°ćŒć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć«ćŖć£ć¦ć„ć¾ć›ć‚“ć€‚

恓悌ćÆ実ćÆnode.jsć®ęŒ™å‹•ć®ē‰¹å¾“恧恙怂ē“°ć‹ć„恓ćØćÆēœē•„ć—ć¾ć™ćŒć€å„ćƒ•ć‚”ć‚¤ćƒ«ļ¼ˆćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ļ¼‰ćÆęš—é»™ć®ć†ć”ć«é–¢ę•°ć«å›²ć¾ć‚Œć¦ć„ć‚‹ćŸć‚ć€varć§å®£čØ€ć—ćŸå¤‰ę•°ćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚¹ć‚³ćƒ¼ćƒ—ć§ćÆćŖććć®é–¢ę•°ć‚¹ć‚³ćƒ¼ćƒ—ć«å±žć™ć‚‹ć“ćØ恫ćŖć‚Šć¾ć™ć€‚ć“ć‚Œć«ć‚ˆć‚Šć€å„ćƒ•ć‚”ć‚¤ćƒ«ć§varć§å®£čØ€ć—ćŸå¤‰ę•°ćŒć‚³ćƒ³ćƒ•ćƒŖć‚Æ惈恙悋恓ćØ恌ē„”恏ćŖ悋ćØć„ć†åˆ©ē‚¹ćŒć‚悋恻恋态__dirnameē­‰ć®å®Ÿč£…恫悂åƄäøŽć—恦恄悋悉恗恄恧恙怂


ćØć“ć‚ć§ć€ä»Šå›žć®č©±é”Œć«é–¢é€£ć™ć‚‹node.js恮ē‰¹å¾“ēš„ćŖęŒ™å‹•ćÆ悂恆1ć¤ć‚ć‚Šć¾ć™ć€‚ćć‚ŒćŒćƒˆćƒƒćƒ—ćƒ¬ćƒ™ćƒ«ć§ć®this恧恙怂

実ćÆ态node.jsē’°å¢ƒć«ćŠć„恦ćÆćƒˆćƒƒćƒ—ćƒ¬ćƒ™ćƒ«ć®ļ¼ˆå…ˆć»ć©čŖ¬ę˜Žć—ćŸć‚ˆć†ć«node.jsć§ć®å„ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ćÆé–¢ę•°ć«å›²ć¾ć‚Œć‚‹ć®ć§åŽ³åÆ†ć«ćÆćƒˆćƒƒćƒ—ćƒ¬ćƒ™ćƒ«ć§ćÆć‚ć‚Šć¾ć›ć‚“ćŒļ¼‰thisćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恧ćÆć‚ć‚Šć¾ć›ć‚“ć€‚ćć®ä»£ć‚ć‚Šć«exportsć‚Ŗ惖ć‚ø悧ć‚Æ惈恌thisć«å…„ć£ć¦ć„ć¾ć™ć€‚

// 恓悌ćÆnode.js恧ćÆtrue
console.log(this === exports);

exportsćØ恄恆恮ćÆCommonJS Modulesē”±ę„ć®ć‚‚ć®ć§ć™ć€‚ć¾ć ć”ć‚ƒć‚“ćØčŖæć¹ć¦ć„ć¾ć›ć‚“ćŒć€ES Modulesćƒ¢ćƒ¼ćƒ‰ć§ćÆć¾ćŸé•ć£ćŸēµęžœć«ćŖ悋恓ćØ恧恗悇恆怂Moduleć‚³ćƒ³ćƒ†ć‚­ć‚¹ćƒˆć®ćƒˆćƒƒćƒ—ćƒ¬ćƒ™ćƒ«ć§ćÆthisćÆundefinedć§ć™ć‹ć‚‰ć€å¤šåˆ†undefined恠ćØę€ć„ć¾ć™ć€‚globalThisćŒčžć„ć¦å‘†ć‚Œć¾ć™ć­ć€‚

globalThisćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恧ćÆćŖ恄ļ¼ļ¼Ÿ

ć”ć‚‡ć£ćØ話ćÆå¤‰ć‚ć‚Šć¾ć™ćŒć€ć¾ćØ悁恮ē›“å‰ćć‚‰ć„ć«ćƒ—ćƒ­ćƒćƒ¼ć‚¶ćƒ«ć‹ć‚‰å¼•ē”Øć—ćŸę–‡ć‚’å†ęŽ²ć—ć¾ć™ć€‚

After some data-gathering to determine web compatibility of a short list of choices, we settled on globalThis, as this name is both highly likely to be web compatible, and also maps to the concept of being the ā€œglobal this valueā€ (and not the ā€œglobal objectā€, per above).

ęœ€å¾Œć®éƒØ分ļ¼ˆē­†č€…ćŒå¤Ŗå­—ć§å¼·čŖæļ¼‰ć«čˆˆå‘³ę·±ć„恓ćØ恌ę›øć„ć¦ć‚ć‚Šć¾ć™ć€‚čŖ­ć‚“恧ćæ悋ćØ态globalThisćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恘悃ćŖ恄悈ćØć„ć†č”ę’ƒēš„ćŖ恓ćØ悒čæ°ć¹ć¦ć„ć¾ć™ć­ć€‚ćŖ悓ćØ态恓恮čؘäŗ‹ć§ć•ć‚“恖悓globalThisćÆć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恠悈ćØčØ€ć£ć¦ććŸć®ćÆå˜˜ć ć£ćŸć®ć§ć™ć€‚

ćØćÆć„ćˆåˆ„ć«å®Ÿå‹™äøŠćŖć«ć‹å•é”ŒćŒć‚ć‚‹ć‚ć‘ć§ćÆć‚ć‚Šć¾ć›ć‚“ć€‚ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恠ćØę€ć£ć¦å®‰åæƒć—恦ä½æć£ć¦ćć ć•ć„ć€‚ęœ€å¾Œć«ć€ć“ć‚ŒćŒčØ€ć„ćŸć„ć®ćÆä½•ć‹ćØ恄恆恓ćØć‚’č§£čŖ¬ć—ćŸć„ćØę€ć„ć¾ć™ć€‚

above恮ćØć“ć‚ć®ćƒŖćƒ³ć‚Æć®å…ˆć§ćÆćŖ恫悄悉WindowProxyćØ恄恆ćƒÆćƒ¼ćƒ‰ćŒå‡ŗć¦ćć¦ć„ć¾ć™ć­ć€‚ć“ć‚ŒćÆćƒ–ćƒ©ć‚¦ć‚¶äøŠć®JavaScript恫恊恑悋windowć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®ć“ćØć‚’ęŒ‡ć—ć¦ć„ć¾ć™ć€‚

ć“ć“ć§ćƒć‚¤ćƒ³ćƒˆćØćŖ悋恮ćÆ态JavaScript恧ćÆä»–ć®ćƒšćƒ¼ć‚øļ¼ˆć‚ˆć‚Šę­£ē¢ŗ恫ćÆćƒ–ćƒ©ć‚¦ć‚øćƒ³ć‚°ć‚³ćƒ³ćƒ†ć‚­ć‚¹ćƒˆļ¼‰ć®windowć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć‚’å–å¾—ć§ćć‚‹ćØ恄恆ē‚¹ć§ć™ć€‚ć“ć‚Œć«ćÆ态window.open悒ä½æē”Ø恙悋ćØ恋态iframe要ē“ ć®contentWindow悒ä½æē”Ø恙悋ćŖć©ć®ę–¹ę³•ćŒć‚ć‚Šć¾ć™ć€‚ć¾ćŸć€å½“ē„¶ćŖćŒć‚‰åŒäø€ć‚ŖćƒŖć‚øćƒ³ćƒćƒŖć‚·ćƒ¼ć®å½±éŸæć‚’å—ć‘ć¾ć™ć€‚

恓悌ćÆćƒ–ćƒ©ć‚¦ć‚øćƒ³ć‚°ć‚³ćƒ³ćƒ†ć‚­ć‚¹ćƒˆć«åÆ¾ć—ć¦ē“ä»˜ć„恦恄悋ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć§ć™ć‹ć‚‰ć€å‚ē…§ć•ć‚Œć¦ć„ć‚‹ćƒ–ćƒ©ć‚¦ć‚øćƒ³ć‚°ć‚³ćƒ³ćƒ†ć‚­ć‚¹ćƒˆå†…ć§ćƒšćƒ¼ć‚ø恌ē§»å‹•ć—恦悂Windowć‚Ŗ惖ć‚ø悧ć‚Æ惈ćÆ偄åœØ恧恙怂恓恮恓ćØ悒ē¢ŗć‹ć‚ć‚‰ć‚Œć‚‹ćƒ†ć‚¹ćƒˆćƒšćƒ¼ć‚ø悒ē”Øę„ć—ćŸć®ć§ć€PCć®ę–¹ćÆ開恍ćŖ恌悉ē¶šćć‚’čŖ­ć‚“ć§ćć ć•ć„ć€‚ć¾ćŸć€ć‚½ćƒ¼ć‚¹ć‚³ćƒ¼ćƒ‰ćÆ適宜参ē…§ć—ć¦ćć ć•ć„ć€‚ļ¼ˆć‚¹ćƒžćƒ¼ćƒˆćƒ•ć‚©ćƒ³ē­‰ć ćØ複ꕰć‚æćƒ–ć‚’č”Œćę„ć™ć‚‹ć®ćŒé¢å€’ć‹ć‚‚ć—ć‚Œć¾ć›ć‚“ć€‚ć¾ćŸć€ć‚½ćƒ¼ć‚¹ć‚³ćƒ¼ćƒ‰ć‚’č¦‹ćŖ恄ćØä½•ć‚’čØ€ć£ć¦ć„ć‚‹ć‹ć„ć¾ć„ć”åˆ†ć‹ć‚‰ćŖć„ć‹ć‚‚ć—ć‚Œć¾ć›ć‚“ć€‚ļ¼‰

ćƒ†ć‚¹ćƒˆćƒšćƒ¼ć‚ø悒開恏ćØindex.html恌č”Øē¤ŗć•ć‚Œć¾ć™ć€‚ć€Œfoo.html悒開恏怍惜ć‚æćƒ³ć‚’ęŠ¼ć™ćØfoo.html恌ꖰ恗恄ć‚æćƒ–ć§é–‹ć‹ć‚Œć¾ć™ć€‚ć‚½ćƒ¼ć‚¹ć‚³ćƒ¼ćƒ‰ć‚’č¦‹ć‚‹ćØ态window.open恮ēµęžœļ¼ˆfoo.html恮Windowć‚Ŗ惖ć‚ø悧ć‚Æ惈ļ¼‰ćŒļ¼ˆindex.htmlå†…ć®ļ¼‰fooWindowå¤‰ę•°ć«ä»£å…„ć•ć‚Œć‚‹ć“ćØćŒåˆ†ć‹ć‚Šć¾ć™ć€‚

恓恮fooWindowå¤‰ę•°ćÆć€ä»Šé–‹ć‹ć‚ŒćŸfoo.htmlå†…ć«ćŠć‘ć‚‹windowć‚Ŗ惖ć‚ø悧ć‚Æ惈ćØåŒć˜ć§ć™ć€‚ćć®ć“ćØćÆ态fooWindowć‚’é€šć˜ć¦foo.htmlå†…ć®ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć‚’å‚ē…§ć—ćŸć‚Šę“ä½œć§ćć‚‹ć“ćØć‹ć‚‰åˆ†ć‹ć‚Šć¾ć™ć€‚

foo.html悒見悋ćØć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°foo恫123ćŒä»£å…„ć•ć‚Œć¦ć„ć‚‹ć“ćØćŒåˆ†ć‹ć‚Šć¾ć™ć€‚äø€ę–¹ć§index.htmlć«ć‚ć‚‹ć€Œfooć®å€¤ć‚’č”Øē¤ŗ怍惜ć‚æćƒ³ć‚’ęŠ¼ć™ćØfooWindow.foo恌č”Øē¤ŗć•ć‚Œć¾ć™ćŒć€ć“ć®ēµęžœćÆ123ćØćŖć‚Šć¾ć™ć€‚ē¢ŗ恋恫foo.htmlå†…ć®ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°fooćŒå‚ē…§ć§ćć¦ć„ć¾ć™ć­ć€‚

恕悉恫态foo.htmlå†…ć«ć‚ć‚‹ć€Œfooć®å€¤ć‚’å¤‰ę›“ć€ćƒœć‚æćƒ³ć‚’ęŠ¼ć™ćØć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°fooć®å€¤ćŒ123456恫ćŖć‚Šć¾ć™ć€‚ćć®å¾Œć€index.htmlć«ęˆ»ć£ć¦å†åŗ¦ć€Œfooć®å€¤ć‚’č”Øē¤ŗ怍惜ć‚æćƒ³ć‚’ęŠ¼ć™ćØ今åŗ¦ćÆ123456恌č”Øē¤ŗć•ć‚Œć¾ć™ć€‚ć“ć®ć“ćØ恋悉悂态fooWindow.foo恌foo.html恮äø­ć®ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°fooćć®ć‚‚ć®ć§ć‚ć‚‹ć“ćØćŒåˆ†ć‹ć‚Šć¾ć™ć€‚

問锌ćÆć“ć“ć‹ć‚‰ć§ć™ć€‚ćƒ–ćƒ©ć‚¦ć‚¶ć®ć‚æ惖ćØ恄恆恮ćÆć‚¦ć‚§ćƒ–ćƒšćƒ¼ć‚ø悒č”Øē¤ŗć—ć¦ćŠć‚Šć€ć‚¦ć‚§ćƒ–ćƒšćƒ¼ć‚øćÆćƒŖćƒ³ć‚Æć‚’ćŸć©ć£ć¦åˆ„ć®ćƒšćƒ¼ć‚ø恫ē§»å‹•ć™ć‚‹ć“ćØćŒć§ćć¾ć™ć€‚ä»Šå›žćÆ态foo.html恫ćÆbar.htmlćø恮ćƒŖćƒ³ć‚Æ恌ē”Øę„ć—ć¦ć‚ć‚Šć¾ć™ć€‚ć§ćÆ态bar.html恫ē§»å‹•ć—恦ćæć¦ćć ć•ć„ć€‚

ēš†ć•ć‚“ćÆ当ē„¶ć”å­˜ēŸ„恋ćØę€ć„ć¾ć™ćŒć€åˆ„ć€…ć®ćƒšćƒ¼ć‚ø恧JavaScriptć®å®Ÿč”ŒēŠ¶ę…‹ćŒå…±ęœ‰ć•ć‚Œć‚‹ć“ćØćÆć‚ć‚Šć¾ć›ć‚“ć€‚åˆ„ć®ćƒšćƒ¼ć‚ø恫ē§»å‹•ć—ćŸę™‚ē‚¹ć§å…Ø恦恮ēŠ¶ę…‹ćÆćƒŖć‚»ćƒƒćƒˆć•ć‚Œć¾ć™ć€‚å½“ē„¶ć€window悂違恆悂恮恫ćŖć‚‹ć—ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć‚‚å…ØéƒØćƒŖć‚»ćƒƒćƒˆć•ć‚Œć¾ć™ć€‚ć“ć®ć“ćØ悒ē¢ŗć‹ć‚ć‚‹ćŸć‚ć«index.htmlć«ęˆ»ć£ć¦ć€Œfooć®å€¤ć‚’č”Øē¤ŗ怍惜ć‚æćƒ³ć‚’ęŠ¼ć—ć¦ćæć¾ć—ć‚‡ć†ć€‚ćć†ć™ć‚‹ćØ态ēµęžœćÆundefinedćØćŖć‚Šć¾ć™ć€‚ć“ć‚ŒćÆ态fooWindow.foo恌ē„”恄恓ćØ悒ē¤ŗ恗恦恊悊态bar.htmlå†…ć«ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°foo恌ē„”恄恓ćØćØåƾåæœć—ć¦ć„ć¾ć™ć€‚

恓恓恧ēŸ›ē›¾ćŒē™ŗē”Ÿć—恦恄悋恓ćØć«ćŠę°—ć„ćć§ć—ć‚‡ć†ć‹ć€‚index.htmlå†…ć®å¤‰ę•°fooWindowćÆåøøć«åŒć˜ćƒ‰ć‚­ćƒ„ćƒ”ćƒ³ćƒˆć§ć™ć€‚ć“ć‚ŒćÆ态index.html恫fooWindowć«å†ä»£å…„ć™ć‚‹ć‚³ćƒ¼ćƒ‰ćŒē„”恄恓ćØ恋悉꘎悉恋恧恙怂

ćć‚Œć«ć‚‚é–¢ć‚ć‚‰ćšć€foo.html恋悉bar.html恫ē§»å‹•ć—ćŸę™‚ē‚¹ć§fooWindow.fooć®å€¤ćŒå¤‰ć‚ć‚Šć¾ć—ćŸć€‚ć¤ć¾ć‚Šć€fooWindowćÆfoo.htmlć®ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć ć£ćŸć®ć«ć€bar.htmlć®ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć«å¤‰åŒ–ć—ćŸćØ恄恆恓ćØ恧恙怂恓悌ćÆ态foo.htmlćØbar.htmlćÆåˆ„ć€…ć®ćƒšćƒ¼ć‚øćŖć®ć§åˆ„ć€…ć®ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈悒ꌁ恤ćØ恄恆äŗ‹å®ŸćØēŸ›ē›¾ć—ć¦ć„ć¾ć™4怂

要ē“„恙悋ćØ态index.html恋悉見悋ćØfooWindowć«å…„ć£ć¦ć„ć‚‹ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćÆå¤‰ć‚ć£ć¦ć„ćŖć„ć®ć«ć€å®Ÿéš›ćÆćƒšćƒ¼ć‚ø遷ē§»ć®éŽēØ‹ć§åˆ„ć®ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć«å¤‰ć‚ć£ć¦ć„ć‚‹ćØ恄恆ēŸ›ē›¾ć§ć™ć€‚

恓恮ēŸ›ē›¾ć‚’č§£ę¶ˆć™ć‚‹ćŸć‚ć«å°Žå…„ć•ć‚Œć‚‹ć®ćŒWindowProxy恧恙怂WindowProxyćÆ内éƒØ恫Windowć‚Ŗ惖ć‚ø悧ć‚Æ惈ćøć®å‚ē…§ć‚’ęŒć£ć¦ć„ć¦ć€åŸŗęœ¬ēš„恫WindowProxyćøć®ę“ä½œćÆ内éƒØ恮Windowćøć®ę“ä½œćØćŖć‚Šć¾ć™5ć€‚ä»Šå›žć®ä¾‹ć§ćÆfooWindowćÆWindowProxyć§ć‚ć‚Šć€ęœ€åˆćÆ内éƒØ恧foo.html恮Windowć‚Ŗ惖ć‚ø悧ć‚Æ惈ćØ恤ćŖćŒć£ć¦ć„ć¾ć™ć€‚ć§ć™ć‹ć‚‰ć€fooWindow恋悉恮čŖ­ćæč¾¼ćæćÆfoo.html恮Windowć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć‹ć‚‰ć®čŖ­ćæč¾¼ćæćØćŖ悊ćŖć‚Šć¾ć™ć€‚

foo.html恌bar.htmlć«ćƒšćƒ¼ć‚ø遷ē§»ć™ć‚‹ćØ态fooWindowćŒå†…éƒØć«ęŒć£ć¦ć„ć‚‹Windowć‚Ŗ惖ć‚ø悧ć‚Æ惈ćøć®å‚ē…§ćŒbar.html恮Windowć‚Ŗ惖ć‚ø悧ć‚Æ惈ćøćØå·®ć—ę›æć‚ć‚Šć¾ć™ć€‚ć“ć‚ŒćÆć‚ćć¾ć§å†…éƒØēš„ćŖå‹•ä½œć§ć‚ć‚Šć€fooWindow恮WindowProxyć‚Ŗ惖ć‚ø悧ć‚Æ惈ćØć—ć¦ć®åŒäø€ę€§ćÆäæć£ćŸć¾ć¾ć§ć™ć€‚恗恋恗态恓恮Ꙃē‚¹ć§fooWindowćøć®ę“ä½œćÆ内éƒØ恮Windowć‚Ŗ惖ć‚ø悧ć‚Æ惈态恙ćŖć‚ć”bar.html恮Windowć‚Ŗ惖ć‚ø悧ć‚Æ惈ćøć®ę“ä½œćØćŖć‚‹ćŸć‚ć€å®Ÿéš›ć«ę“ä½œć•ć‚Œć‚‹Windowć‚Ŗ惖ć‚ø悧ć‚ÆćƒˆćŒęœ€åˆćØē•°ćŖć£ć¦ć„ć¾ć™ć€‚

恓恮2å±¤ę§‹é€ ć«ć‚ˆć‚Šć€å‰čæ°ć®ēŸ›ē›¾ćŒč§£ę¶ˆć•ć‚Œć‚‹ć“ćØ恫ćŖć‚Šć¾ć™ć€‚ēµå±€ć®ćØć“ć‚ä½•ćŒčØ€ć„ćŸć‹ć£ćŸć®ć‹ćØ恄恆ćØ态怌windowćØ恄恆恮ćÆ実ćÆWindowProxy恮恓ćØć§ć‚ć£ć¦ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈恟悋Window恧ćÆćŖć„ć‹ć‚‰ć€ä»Šå›žä½œć‚‹globalThisć‚’ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćØå‘¼ć¶ć®ćÆꆚ悉悌悋怂ćŖ恮恧globalThis恫ćŖć£ćŸć€ćØć„ć†č©±ć§ć—ćŸć€‚

長怅ćØčŖ¬ę˜Žć—ć¾ć—ćŸćŒć€ćŖ悓恠恋ćØć¦ć‚‚ć‚„ć‚„ć“ć—ć„ć§ć™ć­ć€‚ć“ć“ć§ć”ć‚‡ć£ćØč©±ć‚’ęˆ»ć—ć¦ć€globalThisć®ćƒ—ćƒ­ćƒćƒ¼ć‚¶ćƒ«ć‹ć‚‰äø€ę–‡å¼•ē”Øć—ć¾ć™ć€‚

ES6/ES2015 does not account for the Window/WindowProxy structure, and simply refers to ā€the global objectā€ directly. This specification does the same.

ļ¼ˆč¶…ę„čسļ¼‰ ćŖ悓恋HTML恮äŗŗćŸć”ćÆWindowćØ恋WindowProxyćØ恋čØ€ć£ć¦ć‚‹ć‘ć©åˆ„ć«ECMAScriptēš„恫ćÆćć†ć„ć†ć®ć©ćƒ¼ć§ć‚‚ć„ć„ć‚“ć§ć€é¢å€’ćć•ć„ć‹ć‚‰å˜ć«ć€Œć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć€ć£ć¦ć“ćØć«ć—ć¦ć¾ć™ć€‚

ćØ恄恆悏恑恧态äø€åæœåå‰ę±ŗ悁悋ćØćć«ę°—ć‚’ć¤ć‘ćŸćØćÆ恄恈态å…Øćć‚‚ć£ć¦ć©ć†ć§ć‚‚ć„ć„č©±ć§ć—ćŸć€‚ć¾ć•ć«ä½™č«‡ć§ć™ć­ć€‚


  1. č©³ē“°ćÆä»–ć®ć‚‚ć£ćØč©³ć—ć„čؘäŗ‹ć«č­²ć‚Šć¾ć™ćŒć€ECMAScriptć«ę–°ę©Ÿčƒ½ćŒčæ½åŠ ć•ć‚Œć‚‹ć«ćÆStage1 怜 Stage 4ćØ恄恆4恤恮ꮵ階悒ēµŒć‚‹åæ…č¦ćŒć‚ć‚Šć¾ć™ć€‚Stage 3ćÆć€ę©Ÿčƒ½ć®ä»•ę§˜ćŒå›ŗć¾ć‚Šć€ćƒ–ćƒ©ć‚¦ć‚¶ē­‰ć«č©¦éؓēš„ć«å®Ÿč£…ć•ć‚Œć‚‹ć®ć‚’å¾…ć£ć¦ć„ć‚‹ēŠ¶ę…‹ć§ć™ć€‚Ā ā†©

  2. 今恕悉ćŖčŖ¬ę˜Žć§ć™ćŒć€ECMAScriptćØ恄恆恮ćÆć–ć£ćć‚ŠčØ€ć†ćØJavaScript悒ē­–å®šć—ć¦ć„ć‚‹ęؙęŗ–ä»•ę§˜ć®åå‰ć§ć™ć€‚ć“ć®čؘäŗ‹ć§ćÆć€ćƒ–ćƒ©ć‚¦ć‚¶ćØ恋node.jsćØć‹ć§å‹•ćå®Ÿéš›ć®JavaScriptćØåƾęÆ”ć—ć¦ć€ä»•ę§˜ćØć—ć¦å®šć‚ć‚‰ć‚ŒćŸJavaScriptć®ęŒ™å‹•ć«č§¦ć‚ŒćŸć„ćØćć«ECMAScriptćØ恄恆čØ€č‘‰ć‚’ä½æć£ć¦ć„ć¾ć™ć€‚Ā ā†©

  3. 怂恟恠态Chrome AppsćÆ今ćÆChrome OS恮ćæ恌åÆ¾č±”ć‚‰ć—ć„ć§ć™ćŒć€‚Ā ā†©

  4. 恓恮äŗ‹å®Ÿć«ć¤ć„恦ćÆē†ē”±ć‚’čŖ¬ę˜Žć—ć¦ć„ć¾ć›ć‚“ć§ć—ćŸćŒć€HTMLć®ä»•ę§˜ć§ćć®ć‚ˆć†ć«å®šē¾©ć•ć‚Œć¦ć„ć¾ć™ć€‚åˆ„恮HTMLꖇę›øćøć®ćƒŠćƒ“ć‚²ćƒ¼ć‚·ćƒ§ćƒ³ćŒē™ŗē”Ÿć™ć‚‹ćØ态HTMLꖇę›øć‚’ćƒ­ćƒ¼ćƒ‰ć™ć‚‹éŽēØ‹ć§ę–°ć—ć„DocumentćŒä½œęˆć•ć‚Œć€ćć®éŽēØ‹ć§ć‚°ćƒ­ćƒ¼ćƒćƒ«ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćØ恗恦ꖰ恗恄Windowć‚Ŗ惖ć‚ø悧ć‚ÆćƒˆćŒä½œć‚‰ć‚Œć‚‹ć“ćØ恌꘎čØ˜ć•ć‚Œć¦ć„ć¾ć™ć€‚Ā ā†©

  5. 恟恠恗态ć‚ŖćƒŖć‚øćƒ³ć‚’ć¾ćŸćå “åˆćÆåˆ¶é™ćŒć‹ć‹ć‚Šć¾ć™ć€‚ć“ć‚ŒćÆWindowProxyć®å®šē¾©ć§č¦å®šć•ć‚Œć¦ć„ć¾ć™ć€‚Ā ā†©

61
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
uhyo
Metcha yowai software engineer

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
61
Help us understand the problem. What is going on with this article?