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.
what is defference between console.log(this) in nodejs and console.log(this) in browser?
34 Views Asked by Pouya At
2
There are 2 best solutions below
0
Robert Rendell
On
Simple answer to me seems to be, in both examples this is the global object for the JavaScript runtime.
In the browser,
thisis 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,
thisis 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
jsdomin Node.js to get access to the APIs used in a browser DOM https://www.npmjs.com/package/jsdom
Related Questions in JAVASCRIPT
- Using Puppeteer to scrape a public API only when the data changes
- inline SVG text (js)
- An array of images and a for loop display the buttons. How to assign each button to open its own block by name?
- Storing the preferred font-size in localStorage
- Simple movie API request not showing up in the console log
- Authenticate Flask rest API
- Deploying sveltekit app with gunjs on vercel throws cannot find module './lib/text-encoding'
- How to request administrator rights?
- mp4 embedded videos within github pages website not loading
- Scrimba tutorial was working, suddenly stopped even trying the default
- In Datatables, start value resets to 0, when column sorting
- How do I link two models in mongoose?
- parameter values only being sent to certain columns in google sheet?
- Run main several times of wasm in browser
- Variable inside a Variable, not updating
Related Questions in NODE.JS
- Using Puppeteer to scrape a public API only when the data changes
- How to request administrator rights?
- How do I link two models in mongoose?
- Variable inside a Variable, not updating
- Unable to Post Form Data to MongoDB because of picturepath
- Connection terminated unexpectedly while performing multi row insert using pg-promise
- Processing multiple forms in nodejs and postgresql
- Node.js Server + Socket.IO + Android Mobile Applicatoin XHR Polling Error...?
- How to change the Font Weight of a SelectValue component in React when a SelectItem is selected?
- My unban and ban commands arent showing when i put the slash
- how to make read only file/directory in Mac writable
- How can I outsource worker processes within a for loop?
- Get remote MKV file metadata using nodejs
- Adding google-profanity-words to web page
- Products aren't displayed after fetching data from mysql db (node.js & express)
Related Questions in GLOBAL-VARIABLES
- Automatically importing a util module into every file in NodeJS
- How can I use a variable that is defined in one script in a different script in Unity
- Global variable isn't being recognized across functions in python with tkinter
- Why is my function overwriting global variable values that are passed to it?
- Global or accesing global variable datetime string dynamically in web form application using Visual Studio 2022
- What functions can access a global variable that appears in the same file with them?
- I'm having trouble trying to export the data from an object in Flutter using get_it
- Global variable value doesn't change in ISR in C
- Get configuration or global variables into modules definition in NestJS
- How I can reassign value inside of onMount() in sveltekit?
- Best way to use re.sub with a different behavior when first called
- global variables not being updated/ imported properly across modules
- Why does the function always return 0? (function + tkinter + global variables)
- How to set parent variable within a loop in a shell script?
- What could cause linking errors with global variables in header and cpp file?
Related Questions in THIS
- Why does the value of this keyword value is different on normal function vs calling the function using window object
- What is the purpose of a completely standalone 'this'?
- Do all well-known C++ compilers implicitly pass "this" pointer as the 1st parameter to member functions?
- How can an `extension` reference the type of `this` in dart?
- how to pass class name to keyup event in jquery
- Javascript: this is undefined inside jquery click function
- JS: Unexpected error message appear when private method is not accessible after "this unbinding"
- Is there a difference between "one declaration per line" and "multiple variable assignments per line" in Java? or how can the behavior be explained?
- How does GCC handle such a nested lambda where 'this' is getting captured
- TypeError: Cannot set properties of undefined (setting 'channels')
- Constructor call must be the first statement in a constructor-even i have used first statement
- this keyword in dart when combined with $ operator returns unexpected, wierd output
- How to use 'this' pointer with smart pointers
- Typescript interface operating over the same implementation exclusively
- Java example where letting "this" escape during construction results in inconsistent state of an object
Related Questions in WINDOW-OBJECT
- How to know if an external URL is loaded successfully from the main process when using window.open() from the renderer process
- Window.open blocked frame from accessing cross-origin frame
- overflow-x-hidden is weirdly causing "window" object to stop working
- How to access config api url from browser window object?
- window Object getting overwritten using React in Electron
- Inserting react code based on postMessage between two wordpress pages
- Window reference got from window.open() gets closed automatically in Browsers
- what is defference between console.log(this) in nodejs and console.log(this) in browser?
- Why windows.open() is not working as expected?
- Next.js with Firebase Remote Config
- I’m having trouble creating dynamic variables with Window in JavaScript
- Prompt Not working inside for loop in Javascript
- window.location vs window.Location
- In Cypress component tests, changes to window object made inside component render cycle do not persevere
- window.addEventListener('load', function) not working in angular application
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
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