0

Suppose I have an object view. I have a reference to it but I don't control how it's being initialized. It has a property mode. I need to know where from this property is modified for debugging purposes. Since object mutation observer didn't make it to the spec, what are the options now?

I'm thinking two options:

  1. Modify property descriptor to add setter/getter dynamically (requires property being configurable: true).
  2. Use proxy object to intercept value write (requires I substitute original object with proxy during creation)

Are there any other ones I haven't thought of?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • If you're using classes definitely use getters and setters. If you did, then a debugger in the setter for `mode` will show you (up one in the stack) where it was called from. – ZekeDroid Mar 14 '17 at 18:26
  • @ZekeDroid, yeah, that's the idea, it's not limited to classes though, but if object descriptor is set to non configurable, I can't add dynamically getter/setter – Max Koretskyi Mar 14 '17 at 18:29
  • Yeah...the other option is to have a class/object with functions to register listeners, then on a setter call, update the value but before that run` this.notifySubscribers`. And in your case, a subscriber could be a logger or debugger. – ZekeDroid Mar 14 '17 at 18:34
  • @ZekeDroid, thanks, but didn't understand your last suggestion. Can you elaborate? Bear in mind is that I don't control this objects structure at initialization – Max Koretskyi Mar 14 '17 at 18:36
  • Ah crap right, no this won't work then. Without controlling the object I'm not sure it's doable to try and monitor mutations on variable. Especially if the object is frozen. Looking forward to more answers. – ZekeDroid Mar 14 '17 at 18:42
  • @ZekeDroid, yeah. Just tried with getter/setter approach, worked ok. But then if it's freezed, then only proxy option is possible (need to try that one too). Yeah, maybe someone else will offer another solution – Max Koretskyi Mar 14 '17 at 18:44
  • Yeah figured. Also, past tense of "freeze" = "frozen" :) just fyi – ZekeDroid Mar 14 '17 at 18:57
  • @ZekeDroid, yeah, thanks) – Max Koretskyi Mar 14 '17 at 19:00
  • @ZekeDroid The past tense of "freeze" is "froze." "Frozen" is an adjective (and is, like you said, correct). ;) – Jordan Running Mar 14 '17 at 19:24
  • A setter with a `debugger;` breakpoint should suffice. Btw, I wouldn't worry about frozen objects, given that their properties cannot be written anyway :-) – Bergi Mar 14 '17 at 19:35
  • Haha glad you made the correction though to be more correct, frozen can be an adjective, or it can bee the "past participle" as in "This thing was frozen" (verb) as opposed to "This thing is frozen" (adjective). Yes, in this case you are correct though, it is used as an adjective, ten points to Dumbledore! – ZekeDroid Mar 14 '17 at 19:36
  • @Bergi I think the point is that @Maximus is not the one defining the object and is asking how to tell if somewhere in the runtime of their code somewhere at some point called `thatObject.mode = 'new_value'` – ZekeDroid Mar 14 '17 at 19:37
  • @Bergi, yeah, thanks, ‛sealed‛ objects are a problem then :) – Max Koretskyi Mar 14 '17 at 20:34
  • @Maximus Yeah, right. I think that's impossible then. I think I found a suitable duplicate that comes to the same conclusion. – Bergi Mar 14 '17 at 21:29

0 Answers0