在MDN上的Proxy
ownKeys
陷阱文档中,它指出它将拦截Object.keys()
调用:
此陷阱可以拦截以下操作:
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.keys()
Reflect.ownKeys()
然而,从我的测试来看,它似乎无法与Object.keys
一起使用:
const proxy = new Proxy({}, {
ownKeys() {
console.log("called")
return ["a", "b", "c"]
}
})
console.log(Object.keys(proxy))
console.log(Object.getOwnPropertyNames(proxy))
console.log(Reflect.ownKeys(proxy))