I have added my custom object to both Process and Global objects in node js.
global.myObj = Obj
process.myObj2 = Obj2
In some other js file i am using below code to call some methods on above objects
myObj.log('testing....');
process.myObj2.log(' more testing...');
Both are working fine from other js file . Can you please explain the difference between adding object to global and process.
global
is top-level object similar to window
on browser.
process
is object inside global
: global.process
. For more information: https://nodejs.org/api/globals.html#globals_global
I think you're confused between process object v/s global.process object, As JavaScript objects are reference type ,so whether you type process or global.process , it is same thing.
Also, you might be wonder how process object is made available to you directly ! the answer is we can assign reference call process to global.process (like ,process = global.process) . Behind the scene it's done by node for you.
In your case log function's reference is the same (AKA => global.process.myObj and process.myObj2 are same objects)
Try running below code :
console.log(process.env === global.process.env); // true
console.log(global.process.__proto__ === process.__proto__); // true
I hope it helps
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