我试图使用代理拦截get和set操作。设置工作完美,但在无限循环中获取触发“RangeError:超出最大调用堆栈大小”。我试图在拦截期间简单地记录并返回默认行为。
工作:
testobject = {}
testobject = new Proxy(testobject, {
set(target, property, value, receiver) {
console.log("setting", target, property, value, receiver)
return(Reflect.set(target, property, value, receiver))
}
})
testobject.done = true
console.log(testobject.done)
断裂:
testobject = {}
testobject = new Proxy(testobject, {
get(target, property, receiver) {
console.log("getting", target, property, receiver)
return(Reflect.get(target, property, receiver))
},
set(target, property, value, receiver) {
console.log("setting", target, property, value, receiver)
return(Reflect.set(target, property, value, receiver))
}
})
testobject.done = true
console.log(testobject.done)
答案 0 :(得分:0)
我通过移除接收器参数来解决这个问题。
testobject = {}
testobject = new Proxy(testobject, {
get(target, property) {
console.log("getting", target, property)
return(Reflect.get(target, property))
},
set(target, property, value) {
console.log("setting", target, property, value)
return(Reflect.set(target, property, value))
}
})
testobject.done = true
console.log(testobject.done)