Hi, I am trying to transform an object into a proxy, without changing the reference to the object:
(嗨,我正在尝试将一个对象转换为一个代理,而不更改对该对象的引用:)
Here is a simple class:
(这是一个简单的类:)
class Foo {
constructor () {
this.a = 'bar'
}
}
const foo = new Foo()
Here is my Proxy:
(这是我的代理:)
const proxy = new Proxy(foo, {
get () {
return 'proxy-bar'
},
set () {
// Don't do anything
return true
}
})
Here is what a normal use of this proxy does:
(这是此代理的常规用法:)
console.log(foo.a) // bar
console.log(proxy.a) // proxy-bar
Now, I want to keep using foo
, but with the capabilities of proxy
.
(现在,我想继续使用foo
,但要具有proxy
的功能。)
So I tried to use Object.assign
which seems to work only for the getter, but not for the setter. (因此,我尝试使用Object.assign
,它似乎仅适用于getter,但不适用于setter。)
Is their something I miss? (是我想念的吗?)
Object.assign(foo, proxy)
console.log(foo.a) // Getter works: proxy-bar
foo.a = 'proxy-proxy-bar'
console.log(foo.a) // Setter does not work: proxy-proxy-bar
ask by Hammerbot translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…