Skip to content Skip to sidebar Skip to footer

Is It Possible To Pass Parameters Into An Es6 `proxy' Handler?

I want to override base settings with custom settings and the Proxy object seemed like an ideal solution. settingsHandler = { get(target, property) { return this.getS

Solution 1:

I found this gist which helped me get the functionality I wanted by wrapping the Proxy inside a Class and having the settings object be a property of that class:

exportdefaultclassProxySettingsextendsObject {
  token: Token;
  settings: TokenSettings;

  constructor(token, settings) {
    super(...arguments);
    this.token = token;
    this.settings = settings;
    returnnewProxy(this, this.handler);
  }
  gethandler() {
    return {
      get(target, property) {
        return target.token.getSettings(property) || target.settings[property];
      }
    };
  }
}

Which is instantiated like so: this.settings = new ProxySettings(this.token, baseTokenSettings);

Works well but it is a little messy and I haven't seen this pattern before so I am somewhat unsure if this is the best way to do this. Any input appreciated.

Solution 2:

On considering the helpful advice, the simplest solution that fit my needs was to use arrow function syntax:

const settingsHandler = {
  get: (target, property) => {
      returnthis.getSettings(property) || target[property];
  }
}

Post a Comment for "Is It Possible To Pass Parameters Into An Es6 `proxy' Handler?"