I am dealing with a coding challenge that involves managing state in JavaScript. Here is the code snippet I have been working on:
function State(){
const SecretState = {
name: 'bob'
}
this.getState = () => SecretState;
this.setState = (name) => SecretState.name = name;
}
const m = new State()
m.setState('Smith')
m.getState().name = 'Alice'
console.log(m.getState().name)
My goal is to receive "Smith" as the output at the final console log call. However, I also need to restrict the ability to modify the state within the getState
method.