Can anyone shed light on this behavior? and how could a be handled privately as a new object instance? Thanks
var a = {};
var b = function(obj) {
obj.z = 10;
return obj;
};
console.log("---");
console.log(a); // a = {}
b(a);
console.log(a); // a = {z: 10} -- why is a affected? it's not a = b(a);
(function(obj){
obj.x = 9;
console.log(obj);
})(a);
console.log(a); // a = {z:10, x: 9} -- a is also manipulated, why?
EDIT: Objects are accessed by reference therefore "a" is affected globally. Question is, in a Node.js scenario, these objects could be altered by different users entry points if the instance is not isolated / new. ideas on this?