In a closure with internal variables, if I return the variable itself, I'm able to change its value directly. But if I add a getter to the closure, it doesn't output the new set value. But if I use a setter, then the getter outputs the correct value. When I 'change' the value of the returned variable, what exactly am I changing?
function money(){
let usd=10;
const getRate = () => usd;
const setRate = (rate) => usd = rate;
return {usd, getRate, setRate}
}
let m1 = money();
m1.usd = 20;
console.log(m1.getRate()) // Returns 10
console.log(m1.usd) // Returns 20
m1.setRate(30);
console.log(m1.getRate()) // Returns 30