Skip to content

Instantly share code, notes, and snippets.

@xnuk
Created November 3, 2023 18:07
Show Gist options
  • Save xnuk/bc4e9a568ae19943419fa163b1a29f88 to your computer and use it in GitHub Desktop.
Save xnuk/bc4e9a568ae19943419fa163b1a29f88 to your computer and use it in GitHub Desktop.
(() => {
const isObject = t => {
const z = typeof t
return t != null && (z === 'object' || z === 'function')
}
const traceProxy = (target, name = 'object', recursive = false) => new Proxy(target, {
get: (target, prop, receiver) => {
const res = Reflect.get(target, prop, receiver)
console.log('get prop:', name, prop, '==', res)
return recursive && isObject(res) ? traceProxy(res, `${name}[${JSON.stringify(prop)}]`, true) : res
},
apply: (target, that, args) => {
const res = Reflect.apply(target, that, args)
console.log('apply:', that, args, '==', res)
return recursive && isObject(res) ? traceProxy(res, `${name}(...${args.length} args)`, true) : res
}
})
return traceProxy
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment