0

Imagine I have the following Javascript class:

class C {
  constructor() {
    this.foo = "bar"
  }
}

I have proxied C so I can intercept construction of C:

const handler = {
  construct(target, args, newTarget) {
    ...
  }
}

I need to find a way to replace this in this.foo with a proxy. It must be done at the start of the constructor -- I cannot wait for the constructor to return and then proxy the result.

Does anyone have any suggestions on how to do this? Is it even possible?

ebdavis
  • 105
  • 8
  • I'm not 100% sure but I think this can't be done in the exact way you described, but depending on what you want to do with that proxy, did you consider providing an overridden prototype that has getters/setters on the properties that interest you? – CherryDT Sep 14 '22 at 23:56
  • Yeah, I've got workarounds. This would make everything much simpler/nicer though. Alas. Thanks for confirming it! – ebdavis Sep 16 '22 at 19:26
  • Just for completeness, maybe [this](https://stackoverflow.com/a/62774507/1871033) helps – CherryDT Sep 17 '22 at 15:06

0 Answers0