When I log (this) in the browser I get window Object, But when I log (this) in node.js I got {}. I road global object in node js is same window object in browser, What is precisely the deference between this and global Object in Node.js? Thanks for your answers.
Asked
Active
Viewed 29 times
0
-
This is a massive topic, and I would recommend referring to the official documentation, which is [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this). – RAllen May 31 '23 at 08:46
2 Answers
1
Node.js is capable of printing objects without the need to convert them into strings in the console log, while i am guessing in the browser that is not possible. "{}" is an empty object in javascript

Zsombor
- 25
- 7
0
Simple answer to me seems to be, in both examples this
is the global object for the JavaScript runtime.
In the browser,
this
is the Browser Object Model- https://www.w3schools.com/js/js_window.asp
- "All global JavaScript objects, functions, and variables automatically become members of the window object."
- It is a collection of properties and methods that contain information about the browser and computer screen.
- Even the document object (of the HTML DOM) is a property of the window object.
The difference is in the top-level code in a Node module,
this
is equivalent tomodule.exports
. That's the empty object you see.- Meaning of "this" in node.js modules and functions)
- you would need a library called
jsdom
in Node.js to get access to the APIs used in a browser DOM https://www.npmjs.com/package/jsdom

Robert Rendell
- 1,658
- 15
- 18