Value of this in a node module:
this in NodeJS global scope is the current module.exports object, not the global object. This is different from a browser where the global scope is the global window object. Consider the following code executed in Node:
console.log(this); // logs {}
module.exports.foo = 5;
console.log(this); // log { foo:5 }
First we log an empty object because there are no values in module.exports in this module. Then we put foo on the module.exports object, when we then again log this we can see that it now logs the updated module.exports object.
How can we access the global object:
We can access the global object in node using the global keyword:
console.log(global);
The global object exposes a variety of useful properties about the environment. Also this is the place where functions as setImmediate and clearTimeout are located.
Value of this in a node module:
this in NodeJS global scope is the current module.exports object, not the global object. This is different from a browser where the global scope is the global window object. Consider the following code executed in Node:
console.log(this); // logs {}
module.exports.foo = 5;
console.log(this); // log { foo:5 }
First we log an empty object because there are no values in module.exports in this module. Then we put foo on the module.exports object, when we then again log this we can see that it now logs the updated module.exports object.
How can we access the global object:
We can access the global object in node using the global keyword:
console.log(global);
The global object exposes a variety of useful properties about the environment. Also this is the place where functions as setImmediate and clearTimeout are located.
While in browsers the global scope is the window object, in nodeJS the global scope of a module is the module itself, so when you define a variable in the global scope of your nodeJS module, it will be local to this module.
You can read more about it in the NodeJS documentation where it says:
global
<Object> The global namespace object.In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node.js this is different. The top-level scope is not the global scope; var something inside an Node.js module will be local to that module.
And in your code when you write:
console.log(this)in an empty js file(module) it will print an empty object{}referring to your empty module.console.log(this);inside a self invoking function,thiswill point to the global nodeJS scope object which contains all NodeJS common properties and methods such asrequire(),module,exports,console...console.log(this)with strict mode inside a self invoking function it will printundefinedas a self invoked function doesn't have a default local scope object in Strict mode.
What is the purpose of the global object in JavaScript?
Why is there a global object in Javascript? - Stack Overflow
How do JavaScript’s global variables really work?
Isn't Redux just glorified global state?
It's worth keeping in mind that a lot of the advantages that Redux touts, and a lot of the disadvantages associated with Global state, come from the perspective of debugging.
Global state is so maligned because it is notoriously difficult to debug. If any part of your application can modify it at any time, it becomes difficult to figure out which part is making the change that breaks the system.
Redux doesn't suffer from this, because you can easily track the state, and the actions that are applied to it, in order. Which means you can know exactly which Action (the only thing that modifies state in a properly architected Redux/Flux app) is the culprit.
More on reddit.comVideos
I've been reading through the MDN documentation, but it doesn't really explain why this is needed. What actually gets stored in the global window object?
edit:
Found a good answer on stackoverflow!
Why
It's hard to say exactly why this feature exists, but it's useful in some situations. For example, if you're using Webpack or some other packer or ES modules, variables delcared with var will not be stored in the global object and thus will not be accessible through other files. As such, if you want a variable to be available everywhere, you need to declare it directly via the global object (globalThis.varName = value).
Global Scope vs Global Object
As I noted above, variables declared inside ES modules will not be stored in the global object. You can think of the properties on the global object as variables on a "super-global" scope, above the global scope.
While the global scope includes all variables decalerd with var, let or const outside any functions in a file, the global object may or may not include all variables decalerd with var outside any functions in a file.
What ends up in the global object, the scope of the global object and how many global objects there are depends on the environment.
I cannot read the mind of Brendan Eich, but I can take an educated guess as to why the global object exists:
First of all, the design of JavaScript is kept very simple: Records (the place where variable values get stored) and Objects are very similar. Both are basically a collection of key-value pairs. That simplifies the design (e.g. a = 1 works the same way as a.b = 1) and has the side effect that objects can easily be used as records (e.g. with the with statement).
Secondly you might also want to refer to global variables although the variable got shadowed. That can easily be achieved with having a self reference inside the global object / record (window is just a self reference).
Therefore it does make sense that var creates a variable in the global record, which, you can also refer to via window..
Actually that const and let do not create a variable on the global scope is the inconsistency, but with that modularity gets achieved.