我正在尝试调试一些javascript代码,我需要知道何时访问/运行对象上的特定变量。不是在它发生变化时,而是在脚本运行到特定变量时。
有办法吗?
答案 0 :(得分:0)
您可以使用Reflect.apply()
或Proxy.handler()
const o = {
abc: function() {
return 123
},
def: function() {
return "456"
}
}
for (let [key, value] of Object.entries(o)) {
if (typeof value === "function") {
let fn = o[key];
Object.assign(o, {
[key]: function(...args) {
// do stuff
console.log(key + " proxy arguments:", args);
return Reflect.apply(fn, o, args);
}
})
}
}
console.log(o.abc(1, 2, 3), o.def(4, 5, 6));

const o = {
abc: function() {
return 123
},
def: function() {
return "456"
}
}
for (let [key, value] of Object.entries(o)) {
if (typeof value === "function") {
Object.assign(o, {
[key]: new Proxy(o[key], {
apply: function(o, thisArg, args) {
// do stuff
console.log(key + " proxy arguments:", args);
return args;
}
})
})
}
}
console.log(o.abc(1,2,3), o.def(4,5,6));