javascript - 从 getter 获取 Map 代理时接收器不兼容

标签 javascript

我被下面的代码困住了:

class Infrastructure {
  constructor () {
    this._devices = new Map([
    	['foo', 'bar']
    ])
  }
  
  get devices () {
    return new Proxy(this._devices, {})
  }
}

const infrastructure = new Infrastructure()
console.log(infrastructure.devices.get('foo'))

失败并出现以下错误:

Method Map.prototype.get called on incompatible receiver

我知道我需要在某个地方绑定(bind)一些东西,但我不得不承认我有点迷失了。

感谢您的帮助!

最佳答案

这样就可以解决问题了。一旦我有具体的解释就会更新。

class Infrastructure {
  constructor() {
    this._devices = new Map([
      ['foo', 'bar']
    ])
  }

  get devices() {
    return new Proxy(this._devices, {})
  }
};

const infrastructure = new Infrastructure()
console.log(infrastructure.devices.get.bind(infrastructure._devices)('foo'))

关于javascript - 从 getter 获取 Map 代理时接收器不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40795407/

相关文章:

javascript - AngularJS - 将数据添加到现有模型

javascript - 如何使用 JavaScript 在 Canvas 上的棋盘中移动棋子/图像?

javascript - 使 json 字符串与饼图数据源的确切字符串匹配?如何

javascript - 添加 anchor 和 onClick 到链接

javascript - zend jquery ajax 输出与所选值相关的正确值

javascript - rxjs switchmap observable,为最终保存变量

javascript - 我们是否在 webaudio 中使用 createMediaStreamSource 获取所有数据?

javascript - 我不明白 "temp <<= y;"在这个 javascript 程序中做了什么

javascript - 如何使用Javascript替换不规则字符串前缀?

javascript - 为什么 SVG 图像会在同一个 div 中加载重定向?