Reference is making a variable change the value of another

Asked

Viewed 57 times

1

What reason the value returning in the example below is 2 and not null?

const Parametros = {
   Produto: {
      valor: null,
      peso: null,
    },
}

const params = Parametros;
params.Produto.valor = 2;

console.log(Parametros.Produto.valor) //2 deveria ser "null" :/

I would like an explanation and if possible an "ideal" solution to this problem.

Note: I need to use the Parametros as an example of structure in several places.

2 answers

3


Why should it be null if you assigned 2? When you did

const params = Parametros

you just created another reference to the same object. The reference will be const(ante), but the object will not. The object does not suffer "deep copy", it is not duplicated the way you expect.

A trick to do deep copy in Javscript is to do

const params = JSON.parse(JSON.stringify(Parametros))

There yes you can manipulate "params" at will without the Parameter object being affected.

Remembering that deep copy via JSON only works right if the object has been constituted entirely of data (primitive types number, string, date, etc.) If there are objects that cannot be serialized, it will fail.

  • 1

    I understood, I was thinking that the way I did, the object was "copied". Thank you very much!!!

  • It is quite likely that the use of a class would be more appropriate than cloning the object, but there is no way to state without knowing how it is being used.

3

There’s no problem there, it’s happening what the code says to do, if that’s not what you want to do.

You take an object referenced in a variable called Parametros and it says that you want that reference to be placed in a variable called params, so the same object happens to have two references. I imagine you understand what reference is because you put in the title.

So every time you change the referenced object or by params or by Parametros is changing the same object. And of course every time you access this object you will see the change, no matter who has changed.

If you don’t want this, don’t do it like this. Then you need to see what you want.

It may be that you wish to have two different objects. Almost always this is a mistake and may even be the fault of wrong desire. Looking up, without seeing larger context, it doesn’t look like you want to have two objects, but you want behavior that doesn’t make sense.

If you really want two objects you need to think about the copy strategy, even more so that there are levels like this. How much should you copy the object? Everything? Want to choose just a few parts to do it? It starts to get complicated. And worse, if you don’t think about it and think it’s easy you can make a mistake without understanding why.

If you really want this you have one Soen asks how to clone the object in the right way. Cloning the object makes it independent, but it still feels like an error.

Maybe you want to create a prototype and you’re creating an object, you need to define it better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.