3

Javascript proxies are supposed to be the "more general" replacement for Object.observe, but one nice thing about Object.observe was that it let you monitor unintended changes. It could be used as a convenience method for debugging legacy code, for example. Proxies don't seem to function the same way; they only intercept interactions that happen through the proxy. Am I missing something?

rswerve
  • 169
  • 1
  • 3
  • 7
  • `Javascript proxies are supposed to be the "more general" replacement for Object.observe` what makes you say that? – Jaromanda X Sep 12 '17 at 02:36
  • 1
    The docs for Object.observe. `You can use the more general Proxy object instead.` – rswerve Sep 12 '17 at 02:38
  • Sorry, I only read proxy docs :p of course, Object.observe is obsolete, so you shouldn't be using it at all – Jaromanda X Sep 12 '17 at 02:42
  • Correct. But it seems the putative replacement for the obsolete method doesn't offer some of the old functionality. – rswerve Sep 12 '17 at 02:44
  • I see where you're coming from. Can you perhaps post some code you use `Object.observe` for which you can't use proxy instead? Maybe someone will be able to help with a specific example – Jaromanda X Sep 12 '17 at 02:47
  • The linked docs actually have good examples. You can put an observer directly on an object, and log out any changes. Proxy requires interactions to go through the handler. – rswerve Sep 12 '17 at 02:49
  • however, couldnt all of the `Object.observe` cases be covered in a handler. you could create a `get` a `set` handler that logs every property accessed, and wrap it on an existing library like say jquery `jquery = new Proxy(jquery, handler)` and get the same result. – Ja Superior Dec 28 '17 at 06:11
  • 3 years later, still no replacement to `Object.observe` – David Alsh Nov 17 '20 at 10:22
  • The main difference that I see is that with the Proxy you need to use the proxy for manipulation, whereas Object.observe lets you manipulate the original object, which is exactly what I need. Getter and setter also don't work for me since you have to explicitly set up the original object with getters, whereas Object.observe lets you register multiple handlers after the fact. – Haeri Dec 02 '21 at 14:19

1 Answers1

0

5 years later, the answer is no, they don't. Unfortunately, a proxy will only track the access to the proxy itself which is quite useless when you need to "spy" an existing object.

You could still try to create a proxy from an existing object and then replace that existing object with the proxy you just created from, but this is risky as there no warranty you will be replacing it everywhere.

Note: if I really need to observe the properties of an object i.e. to know when they changed, I'm using this helper. This is not plain JS as it make use of rxjs, but it works pretty well.

usage (typescript):

const obj: {key: string} = {key: "value"};
observeProperty$(obj, 'key').subscribe((nextValue)=>console.log(nextValue));

obj.key = "anotherValue"

// console output: "anotherValue"
Flavien Volken
  • 19,196
  • 12
  • 100
  • 133