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:
export default class ProxySettings extends Object {
token: Token;
settings: TokenSettings;
constructor(token, settings) {
super(...arguments);
this.token = token;
this.settings = settings;
return new Proxy(this, this.handler);
}
get handler() {
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) => {
return this.getSettings(property) || target[property];
}
}
Post a Comment for "Is It Possible To Pass Parameters Into An ES6 `Proxy' Handler?"