eolas/zk/Global_object_in_NodeJS.md

28 lines
1.1 KiB
Markdown
Raw Normal View History

2022-04-23 13:26:53 +01:00
---
tags:
- node-js
---
2022-07-16 10:30:04 +01:00
# Global object
2022-04-23 13:26:53 +01:00
> In Node every function and variable should be scoped to a module. We should
> not define functions and variables within the global scope.
2022-04-23 13:26:53 +01:00
- In Node the equivalent to the browser's `Window` object is `global`. The
properties and methods that belong to this method are available anywhere in a
program.
2022-04-23 13:26:53 +01:00
- Just as we can technically write `Window.console.log()`, we can write
`global.console.log()` however in both cases it is more sane to use the
shorthand.
2022-04-23 13:26:53 +01:00
- However if we declare a variable in this scope in browser-based JavaScript,
this variable becomes accessible via the `Window` object and thus is
accessible in global scope. The same is not true for Node. If you declare a
variable at this level it will return undefined.
2022-04-23 13:26:53 +01:00
- This is because of Node's modular nature. If you were to define a function
`foo` in a module and then also define it in the global scope, when you call
`foo`, the Node interpreter would not know which function to call. Hence it
chooses not to recognise the global `foo`, returning undefined.