if I have foo = 1
and bar = foo + 1
, how would I go about setting up a listener for changes in foo so that bar automatically updates when the value of foo changes?
Asked
Active
Viewed 27 times
0
![](../../users/profiles/2719545.webp)
Quilty Kim
- 397
- 3
- 12
-
There was [`Object.watch()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch) some time ago – Luca Kiebel May 05 '18 at 13:23
-
How does foo change? Does it come from a databese? Or is it bound to a Dom element? There must be a reason why foo changes. Why don't you listen to that? – Rüzgar May 05 '18 at 13:24
-
But if you want to detect when an non-object changes, I don't believe there is a way of doing that. Well, actually if it's still a property of `window`, then maybe you could... – pushkin May 05 '18 at 13:27
-
both `foo` and `bar` would be primitives and `foo` would be updated through a click event on the DOM. I currently have it setup so that the function that updates `foo` also updates `bar` by repeating `bar = foo + 1` through reassignment. I wasn't sure if something like Rxjs would be appropriate for this use-case. I'm also open to refactor these variables to be properties of an object if that makes what I'm describing more feasible. – Quilty Kim May 05 '18 at 13:31