【发布时间】:2021-07-05 18:58:55
【问题描述】:
如果您尝试调试定义了许多方法的类的实例,则在 IDE(VSCode 或 WebStorm)中调试 JavaScript 可能会变得很困难:
如您所见,每个 Vector 大约有 15 种方法。我有兴趣只查看实例属性(x、y 等)并在调试时隐藏方法。每个实例的方法都相同,并且不相关。这使得调试变得困难:这似乎是一个小问题,但如果您需要调试长时间的“大”实例会话,您可能会迷路。
有没有办法(通过 IDE,或通过其他设置)在 IDE 调试器上过滤实例方法?
如果我能做到这一点,我可以看到 x、y 值内联,这可以节省我的时间,目前,IDE 中的内联预览被不相关的函数签名所淹没。
替代方案:
有没有办法编辑调试器的内置预览功能?
我可以覆盖 console.log like this 但不会影响 IDE 预览:
const tempConsoleLog = console.log;
console.log = (...argss) => {
function clear(o) {
var obj = JSON.parse(JSON.stringify(o));
// [!] clone
if (obj && typeof obj === 'object') {
obj.__proto__ = null;
// clear
for (var j in obj) {
obj[j] = clear(obj[j]); // recursive
}
}
return obj;
}
for (var i = 0, args = Array.prototype.slice.call(argss, 0); i < args.length; i++) {
args[i] = clear(args[i]);
}
tempConsoleLog.apply(console, args);
};
我仍在寻找以某种方式破解 IDE 预览的方法...
编辑:
向量类:
export class Vector {
x: number;
y: number;
faceDirs: Dir[]; // all allowed dirs
_chosenFaceDir: Dir; // chosen dir
dir: Dir;
constructor(x: number | Vector, y?: number) {
if (x instanceof Vector) {
this.x = x.x;
this.y = x.y;
if (typeof y === 'number') throw Error('illegal');
} else {
this.x = x;
this.y = y as number;
}
this.faceDirs = null;
this._chosenFaceDir = null;
if (!(this instanceof Dir)) this.dir = new Dir(this.x, this.y);
}
// eq = (p: Vector) => p.x === this.x && p.y === this.y;
eq = (p: Vector) => eq(p.x, this.x) && eq(p.y, this.y);
notEq = (p: Vector) => !eq(p.x, this.x) || !eq(p.y, this.y);
add = (p: Vector | number) => operatorFunc(this, p, operators.add);
sub = (p: Vector | number) => operatorFunc(this, p, operators.sub);
mul = (p: Vector | number) => operatorFunc(this, p, operators.mul);
dev = (p: Vector | number) => operatorFunc(this, p, operators.dev);
absSize = () => Math.sqrt(this.x ** 2 + this.y ** 2);
size = () => this.x + this.y;
abs = () => new Vector(Math.abs(this.x), Math.abs(this.y));
【问题讨论】:
-
WebStorm: youtrack.jetbrains.com/issue/WEB-24515, youtrack.jetbrains.com/issue/WEB-27621 我猜是链接票。 P.S. 我不明白为什么
console.log()会影响调试器:console.log不会向实际的 JS 调试器报告任何内容;调试器向实际的“执行环境”查询它有哪些变量等。 -
"每个实例的方法都是相同的并且不相关。" - 那么你的类应该在原型中定义它们,而不是为每个实例分配单独的函数。您的调试器告诉的是它们 是 实例属性,与
x和y完全相同(除了保存函数对象而不是数字)。它们甚至可能在构造函数中在它们之前创建,因为调试器保持创建顺序。请向我们展示您的class Vector声明。 -
@Bergi 我添加了 Vector 类,我应该如何在 proto 中定义方法?
-
@EliavLouski 使用正确的方法定义,而不是arrow functions in class fields。
-
谢谢@Bergi,它为我解决了这个案例的问题。你可以把它写成答案,我会接受它
标签: javascript debugging visual-studio-code webstorm