# global var / function is global object property❗️

* [JS](https://lochiwei.gitbook.io/web/js) ⟩ [scope](https://lochiwei.gitbook.io/web/js/scope) ⟩ [global](https://lochiwei.gitbook.io/web/js/scope/global) ⟩ [variable](https://lochiwei.gitbook.io/web/js/scope/global/variable) ⟩ [var](https://lochiwei.gitbook.io/web/js/scope/global/variable/var) ⟩ is global object property
* [JS](https://lochiwei.gitbook.io/web/js) ⟩ [variable](https://lochiwei.gitbook.io/web/js/variable) ⟩ [var](https://lochiwei.gitbook.io/web/js/variable/declare/var) ⟩ global var is global object property

{% hint style="warning" %}
(<mark style="color:red;">**side effect**</mark>)

[var](https://lochiwei.gitbook.io/web/js/scope/global/variable/var "mention")/ <mark style="color:yellow;">**global**</mark> [func](https://lochiwei.gitbook.io/web/js/val/func "mention") are <mark style="color:yellow;">**implemented**</mark> as [prop](https://lochiwei.gitbook.io/web/js/scope/global/object/prop "mention"):exclamation:&#x20;
{% endhint %}

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

* [ ] [ydkjs-scope-and-closures-v.2](https://lochiwei.gitbook.io/web/master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2 "mention") ⟩&#x20;
  {% endtab %}

{% tab title="👥 相關" %}

* :white\_check\_mark:[stop-using-var](https://lochiwei.gitbook.io/web/js/variable/declare/var/stop-using-var "mention")
* :beginner:[global](https://lochiwei.gitbook.io/web/js/scope/global "mention")
* :exclamation:[var](https://lochiwei.gitbook.io/web/js/scope/global/variable/var "mention") is a [prop](https://lochiwei.gitbook.io/web/js/scope/global/object/prop "mention"), and vice versa.
* :scales: <mark style="color:yellow;">**compare**</mark>： [module](https://lochiwei.gitbook.io/web/js/scope/module "mention") - no module-wide scope object.
  {% endtab %}

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

* replit：[global var is global object property](https://replit.com/@pegasusroe/global-var-is-global-object-property#index.js)

```javascript
const { log } = console;
const isCurrentlyInGlobalScope = () => this === globalThis;

// log( prop );                // ReferenceError: (property is not hoisted) ⛔ 
log( topLevelVar );              // undefined     : (var is hoisted but uninitialized)

// ⭐️ a global object property is like a global var (but not hoisted)
globalThis.prop = 42;            //                // define new property
log(prop, typeof prop);          // 42, number     // it acts like a global var
prop = 36;                       //                // we can change it
console.log(globalThis.prop);    // 36             // change will reflect on global object

// ⭐️ a global var is a global object property

// ❗ but in Node.js:
//    • top-level scope is NOT global scope
//    • top-level var will NOT be reflected in global/module context.
var topLevelVar = 'hello';
log(globalThis.topLevelVar);     // undefined❗
log(module.exports.topLevelVar); // undefined❗

// log
;[
    globalThis === global,        // true

    // current context
    isCurrentlyInGlobalScope(),   // false
    this === module.exports,      // true

].forEach(x => console.log(x));
```

{% endtab %}

{% tab title="🗣 討論" %}

* [Is there any difference between a global variable and a property of the Global Object](https://stackoverflow.com/questions/12439256/is-there-any-difference-between-a-global-variable-and-a-property-of-the-global-o)
  {% endtab %}

{% tab title="🚧" %}

* [ ] global function
  {% endtab %}
  {% endtabs %}
