我被下面的代码困住了:
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/