2
I have the following code, and in the change function, I want to pass a variable as a parameter, not its value, and I don’t know how to do that. Because I want the function to do the "Listen" of the variable, and not of its value, so when I assign a new value to the status variable, the change event will be triggered.
let status = true;
function onChange(value, callback) {
let aux = value;
setInterval(() => {
if(value != aux) {
aux = value;
callback();
}
}, 100)
}
onChange(status, () => {
console.log('Valor Mudou');
});
status = false;
//a saída ira disparar o evento de mudança, percebendo que a variável status agora é falsa, entretanto isso não acontece pois ele está pegando o valor dela, e não a variável em si.
You couldn’t just use get/set?
– Francisco
@Francisco, get/set would actually solve it, but then you’d need to rewrite all the attributes that I want to do Isten, creating your getters and setters. And I didn’t want to have to rewrite, so I’m trying to do something more generic, I don’t know if it’s possible, but that would be it.
– Raizant
I think the only way you can do that is with getters and Setter or with Proxy
– Costamilam
If you only have objects, you can pass their value with the reference of the original object. Then you could implement the solution that is in the question.
– Francisco
Yes @Francisco, the problem is that I am doing this for a long and old code, I would have to rewrite all the variables, the methods that use these variables, and so on. You’d have to turn them into objects, create get/set. In the end this will increase my work, this way, I would make a specific Listen for each variable: statusOnChange() and inside would say that I want to verify the value of the status variable, however as I said, I wanted to do something more generic, that could be reused. By the amount of code, at the moment get/set will not help me. :(
– Raizant