"call-by-sharing"

Tell us what’s happening:

I sorted out the problem but think it would be useful to add something here. My code below is obviously incorrect because it attempts to mutate the state. However, when I was doing so, I was thinking that JS must pass parameters by value so it is safe to do so. And I found this:

It seems the same case for Python.

  **Your code so far**
const defaultState = {
login: false
};

const reducer = (state = defaultState, action) => {
// Change code below this line

if (action.type === 'LOGIN') {
  state.login = true;
}

return state;
// Change code above this line
};

const store = Redux.createStore(reducer);

const loginAction = () => {
return {
  type: 'LOGIN'
}
};
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Challenge: Handle an Action in the Store

Link to the challenge:

It does always pass by value, and it does that by making a copy of the thing you’re passing. But the thing you’re passing (state) is an object reference, not the values inside the object: you’re passing the object to the function, which assigns a copy of that reference to a variable called state, which it uses inside the function. It points at the exact same things that the object contains though.

Many languages do this, because otherwise it would be enormously expensive, computationally, to do anything involving objects.

Is this the case: All objects are passed by reference, and the rest are passed by value?

The value is the reference. The top answer in the SO thread puts it like this.

Instead, the situation is that the item passed in is passed by value. But the item that is passed by value is itself a reference. Technically, this is called call-by-sharing.

Then how do I know whether it is a reference or value? In C++ we can use & to explicitly tell the parameter is a reference and in all other cases it is a value. What is the equivalent way to do so here?

You know the value is a reference by looking at the call site or doing a type check on the parameter. If the value is an object type it is a reference.

If you want type checking you need TypeScript.

No, pass by reference is impossible in JS, JS doesn’t support it, it doesn’t work that way. Pass by reference means you pass the actual memory address.

When you pass an object to a function, the value that gets assigned to the parameter variable is a copy of an identifier for that object. It’s not the items defined as being properties of the object, it’s a value that includes references to those items.

Practically it isn’t going to make a huge amount of visible difference. JS allows you to freely add/remove/change object properties and their values. So if you pass an object, even though it’s a value you’re passing, that value is just the object, not the values of the properties on the object. Note that in this case, this particular function, the entire point is to return a brand new object

Thanks everyone, I read a bit more (esp. in the stack overflow post I attached) and got a sense of what is happening now.