When I call the method the value of the variable changes even though I will assign another variable.
app.js
var methods = require('./module');
var ObjectExample = {};
ObjectExample['name'] = 'NODE';
ObjectExample['array'] = [];
ObjectExample['array'].push(1);
methods.test1( ObjectExample );
methods.test2( ObjectExample );
module.js
module.exports.test1 = function( ObjectExample ){
var parameters = ObjectExample;
parameters['name'] = 'NODE.JS';
parameters['array'][0] = 2;
};
module.exports.test2 = function( ObjectExample ){
console.log( ObjectExample ); // {name:'NODE.JS', array:[2]}
};
Why can I do this in the module test2.
{ name: 'NODE', array: [1] }
This is same object you are passing to both functions. So when you change it in one module, it changed forever in all iterations of this object. You may clone this object to avoid such behaviour.
Set value of class variable
module.exports.test1 = function( ObjectExample ){
var parameters = ObjectExample;
parameters['name'] = 'NODE.JS';
parameters['array'][0] = 2;
this.ObjectExample = parameters;
};