2

I have an object, which I modify on a regular basis. As data is discovered and discarded, I add items to and delete items from the nested arrays.

let obj = {
  image: [],
  video: []
}

I am wondering if there is anything like MutationObserver that is instead capable of monitoring changes to objects?

If not, is there a way to monitor an object for changes without manually polling it constantly?

oldboy
  • 5,729
  • 6
  • 38
  • 86
  • 2
    Proxy [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) – 1252748 Jan 24 '20 at 23:00
  • The way I've seen a lot of systems handle this problem is by using explicit `set` methods and even those have their problems in detecting changes. For instance `var x = {stuff: []}; var y = x.stuff; y[0] = 'thing'; x.set('stuff', y);` Most "setter" systems fail to detect that change as the underlying array is still the same object. – gforce301 Jan 24 '20 at 23:01
  • @gforce301 is it possible to do in a foolproof manner tho? or at least if i determine how the arrays are modified? – oldboy Jan 24 '20 at 23:07
  • It just depends on how you define "foolproof" and what level of "un-efficiency" you're willing to accept. Even using a proxy to trap the setting of property values suffers from the same problem as in my comment above. – gforce301 Jan 25 '20 at 02:04
  • @gforce301 u got me scared, so i found a way around it which is sufficient for the time being :) – oldboy Jan 25 '20 at 04:18

0 Answers0