Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.object vs global.object

Tags:

node.js

I have added my custom object to both Process and Global objects in node js.

app.js

global.myObj = Obj  
process.myObj2 = Obj2

In some other js file i am using below code to call some methods on above objects

file1.js

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.

like image 698
user1814841 Avatar asked Dec 23 '15 11:12

user1814841


2 Answers

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

like image 167
trquoccuong Avatar answered Oct 13 '22 01:10

trquoccuong


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

like image 40
Meet Patel Avatar answered Oct 13 '22 01:10

Meet Patel