1
I want when one value is changed within one object of a certain class, another to change as well. Example:
class minhaClasse{
constructor(valor1, valor2){
this.valor1 = valor1
this.valor2 = valor2
this.valores = {
valor1: this.valor1,
valor2: this.valor2
}
}
}
var meuObjeto = new minhaClasse(2,4)
console.log(meuObjeto.valor1, meuObjeto.valores)
meuObjeto.valor1++ // ou meuObjeto.valor1 = 3
//log esperado => 3 {"valor1": 3, "valor2": 4}
console.log(meuObjeto.valor1, meuObjeto.valores)
They just answered me on Stackoverflow gringo, they answered the same thing as you. Anyway, thank you very much!
– Luís HNrique
@Luíshnrique both at the same time!. For, getters is the simplest way and as I wrote in the answer these values cannot be changed if configured in the correct way.
– Sergio
I had tried to use
get valor1(){ return this.valor1 }
, had not thought to create a pro variablethis
– Luís HNrique