Skip to content Skip to sidebar Skip to footer

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 Prox

Solution 1:

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 = {
  getgetProp() {
    return"obj getter: " + (this["x"] || void0);
  }
};

var proxy = newProxy(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;

console.log(obj.x);

console.log(obj.getProp);

Post a Comment for "Is Javascript Proxy Supposed To Intercept Direct Changes To Underlying Object Like Object.observe?"