Why does howdothisvargetchanged get a different value after parsing it to changevar?
How can it be changed by the function when the variable only exist outside of the scope?
(function()
{
var howdoesthisvargetchanged = {'a': 'a', 'B': 'B', 'C': 'C'};
console.log(JSON.stringify(howdoesthisvargetchanged));
changevar(howdoesthisvargetchanged);
console.log(JSON.stringify(howdoesthisvargetchanged));
function changevar(v)
{
v['C'] = 'why does this work?';
}
})();
It works because you are passing
howdoesthisvargetchangedby reference tochangevar(). If you tried to re-assign the variable inchangevar()nothing would happen externally. But since we have a reference to an existing object we are acting on that instance. Any changes we make to that object’s properties will be reflected to anything else accessing that object.