Because obj.js is exporting an object literal. The first time you require obj.js the object is instantiated, and assigned memory allocation. Every time you require it, it's the same object, from memory. If obj.js was exporting a function that returned an abject, well that would be different.
Can you please clarify?
Let's say we have three files:
Console output after run
node index.js
:It's clear that
index.js
andbar.js
share the same object fromobj.js
.Not like you should be changing modules anyways...
Because
obj.js
is exporting an object literal. The first time you requireobj.js
the object is instantiated, and assigned memory allocation. Every time you require it, it's the same object, from memory. Ifobj.js
was exporting a function that returned an abject, well that would be different.Agree. But in this case the exported function will be the same for all modules (i.e. the function will not be copied for every import).
Right