I've just seen a weird behaviour of the this
keyword in NodeJS environment. I'm listing them with code. I've run this codes with NodeJS v6.x
, with a single JavaScript
file.
While testing with one line of code as follows, whether with or without the 'use strict'
statement, this points to an empty object {}
.
console.log(this)
But, when I'm running the statement within a self executing function like,
(function(){ console.log(this); }());
It's printing a really big object. Seems to me the Global execution context object created by NodeJS
environment.
And while executing the above function with a 'use strict'
statement, expectedly it's printing undefined
(function(){ 'use strict'; console.log(this); }());
But, while working with browser (I've tested only with Chrome
), the first three examples yield the window
object and the last one gave undefined
as expected.
The behaviour of the browser is quite understandable. But, in case of NodeJS
, does it not create the execution context, until I'm wrapping inside a function?
So, most of the code in NodeJS
runs with an empty global object
?
Node. js global objects are global in nature and they are available in all modules. We do not need to include these objects in our application, rather we can use them directly. These objects are modules, functions, strings and object itself as explained below.
A global object is an object that always exists in the global scope. In JavaScript, there's always a global object defined. In a web browser, when scripts create global variables defined with the var keyword, they're created as members of 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.
The Node object represents a single node in the document tree. A node can be an element node, an attribute node, a text node, or any other of the node types explained in the Node Types chapter.
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: First we log an empty object because there are no values in module.exports in this module.
Global Objects are built-in objects that are part of the JavaScript and can be used directly in the application without importing any particular module. The Node.js Global Objects are listed below:
The objects listed here are specific to Node.js. There are built-in objects that are part of the JavaScript language itself, which are also globally accessible. No longer experimental. A utility class used to signal cancelation in selected Promise -based APIs. The API is based on the Web API AbortController.
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. <Object> The global namespace object.
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.
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, this
will point to the global nodeJS scope object which contains all NodeJS common properties and methods such as require()
, module
, exports
, console
...console.log(this)
with strict mode inside a self invoking function it will print undefined
as a self invoked function doesn't have a default local scope object in Strict mode.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With