While working on a NodeJS project, I came across this very unexpected behavior that I canβt understand the way - it seems to me that this is a mistake, but maybe I just donβt understand how NodeJS modules work.
I brought it to the test file as follows:
mod.js module
exports.process = function (obj) {obj.two = 'two'; }; file test.js
var testObj = {one: 'one'};
console.log (['Before:', testObj]);
var cachedObj = testObj;
require ('./ mod'). process (cachedObj);
console.log (['After:', testObj]);
Then running $ node test.js gives me the following:
['Before:', {one: 'one'}]
['After:', {one: 'one', two: 'two'}] I assign the value testObj cachedObj and testObj never passed to the modular method. testObj should (as far as I see) never change at all.
In fact, cachedObj will certainly never be changed since it never returns from the mod.process method. Where am I mistaken?
(running Node 0.6.9)
source share