是否可以将参数传递给ES6“代理"处理程序? [英] Is it possible to pass parameters into an ES6 `Proxy' handler?

查看:46
本文介绍了是否可以将参数传递给ES6“代理"处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用自定义设置和

I want to override base settings with custom settings and the Proxy object seemed like an ideal solution.

  settingsHandler = {
    get(target, property) {
        return this.getSettings(property) || target[property];
    }
  }
  this.settings = new Proxy(baseSettings, settingsHandler);

,但是处理程序中的 this 上下文只是 target 对象,而 this.getSettings(property)失败,因为它不可用

but the this context inside the handler is just the target object, and this.getSettings(property) fails because it is not available.

似乎不是 Proxy 的意思,但我找不到明确的表述.也许我应该去上课吗?

Seems like Proxy is not meant for this but I cannot find that stated unequivocally. Perhaps I should make a class instead?

推荐答案

在考虑有用的建议时,最符合我需要的最简单的解决方案是使用箭头函数语法:

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];
  }
}

这篇关于是否可以将参数传递给ES6“代理"处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆