alpav alpav - 1 year ago 88
Javascript Question

Is JavaScript Proxy supposed to intercept direct changes to underlying object like Object.Observe?

MDN for Object.Observe says that Observe is now obsolete and we should use "more general Proxy object instead".

But Observe allowed to intercept changes on existing object. If Proxy does not allow then Proxy API is not "more general" than Observable API.

MDN for Proxy and this question give examples of intercepting changes to proxy object, but never talk about changes to underlying object.

Is Proxy supposed to intercept changes to underlying object in current or future ECMA standard ?


let o = {};
let p = new Proxy(o, { set: (target, prop, val)=> console.log(target, prop, val) });
p.p1 = 'v1'; // this will log o, "p1", "v1"
o.p2 = 'v2'; // is this supposed to log o, "p2", "v2" in ECMA standard ?

Answer Source

o.p2 = 'v2'; // is this supposed to log o, "p2", "v2" in ECMA standard ?

No, using that particular pattern.

Set the value at the Proxy object, and value will be set at target object.

Though you can also define getter at original object.

var obj = {
  get getProp() {
    return "obj getter: " + (this["x"] || void 0);

var proxy = new Proxy(obj, {
  set: function(obj, prop, newval) {
    var oldval = obj[prop];
    console.log("set", oldval, obj, prop, newval);
    obj[prop] = newval;
  }, get: function(obj, prop) {
       console.log("get", obj, prop);

proxy.x = 1;



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download