There's a JavaScript function, lets call it "primary", it contains another function inside, name it "secondary".
The logic is that primary gets an object, passes the object to secondary then secondary adds another key/value into given object, finally primary returns the object.
function primary(objA) {
function secondary(obj, key, value) {
obj[key] = value;
}
let key = 'newkey';
let value = 'newvalue';
secondary(objA, key, value);
return objA;
}
Surprisingly, objA
is changed.
{
key: "value",
newkey: "newvalue"
}
My question is, how is the secondary function changing obj without returning the modified object!?
Is it because secondary is in the scope of primary? I need a complete explanation please.