0
class dataset {
  __data__={};
  __actions__ = {};
  __data__ = new Proxy(this.__data__,{
    set(target,prop,val){
      this.__actions__[prop]?this.__actions__[prop]():'';
      target[prop]=val;
    },
    get(t,p,r){
      return t[p];
    }
  });
  setData(prop,val){
    this.__data__[prop] = val;
  }
  getData(prop){
    return this.__data__[prop];
  }
  watch(prop,action=()=>{}){
    this.__actions__[prop] = action;
  }
}
x = new dataset;
x.parent=dataset;
x.setData('name','satyam');
let y = x.getData('name');
console.log(y);

I can't get the scope of the dataset class in the Proxy to get actions.

I am trying to make an class which takes an corresponding action when data in it is changed

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • The value of `this` inside of your get trap refers to your proxy handler object, not your class. You can use an arrow function to take the `this` from the surrounding scope (ie: your class) – Nick Parsons Jun 20 '22 at 11:27
  • Thanks Nick, your answer gave me idea, i created a method in the dataset class which first reffered the context and then returned the object for proxy... – Satyam Bharti Jun 20 '22 at 11:59

0 Answers0