I seem to be missing a fundamental concept in programming as I am encountering an unusual issue that I have never faced before.
Let me illustrate my problem through this code snippet:
var result = {abc: 10, cde: 20, efg: 30};
var final_result = {};
var customFunction1 = function(results){
console.log(results);
return results; // result= {abc: 10, cde: 20, efg: 30}
};
var customFunction2 = function(results){
results.cde = 100;
results.efg = 500;
return results; // {abc: 10, cde: 100, efg: 500}
};
final_result.result1 = customFunction1(result);
final_result.result2 = customFunction2(result);
console.log(final_result);
In the code above, I'm passing the "result" object as a parameter to functions and storing the returned value in "final_result.result1". However, this value gets overwritten when I call another function with the same parameters. The output I am currently receiving is:
{"result1":{"abc":10,"cde":100,"efg":500},"result2":{"abc":10,"cde":100,"efg":500}}
The expected output should be: {"result1":{"abc":10,"cde":20,"efg":30},"result2":{"abc":10,"cde":100,"efg":500}}
I'm puzzled why the value of "final_result.result1" is being overwritten by "result.result2".
JSBin http://jsbin.com/mepizecuka/edit?js,console
Plunkr http://plnkr.co/edit/BF0UNnacV9UeXtyk3stI?p=preview
If anyone could provide some assistance, it would be greatly appreciated.