edit: I think I have seen this problem when using chrome to debug my full application. So, not sure if this is a typescript issue or a VScode issue.
I was about to submit when I thought I should try it using chrome as the debugger instead of VS code. Chrome works as expected, but VS code shows the problem illustrated below.
I distilled the following down from a larger program that was giving me some strange behavior when examining things in the debugger. The program appears to work correctly in terms of what it prints out, but if I run it in VS code or attach to a running process, when inspecting the value of this inside somePrivateArrowFunc, I see different results in the debugger than are printed out to the console:
class MyClass {
someField: number = 123;
private somePrivateArrowFunc = () => {
console.log("somePrivateArrowFunc", this);
};
funcRefs = [this.somePrivateArrowFunc];
funcRef = this.somePrivateArrowFunc;
public somePublicRegularFunc() {
console.log("somePublicRegularFunc", this); // debugger sees "this" as instance of MyClass
for (let f of this.funcRefs) {
f(); // debugger sees "this" as the global object inside somePrivateArrowFunc
}
this.funcRefs[0](); // debugger sees "this" as an array containing f inside somePrivateArrowFunc
this.funcRef(); // debugger sees "this" as an instance of MyClass inside somePrivateArrowFunc
}
}
var c:MyClass = new MyClass();
c.somePublicRegularFunc();
The output printed to the console indicates that the value of this is always an instance of MyClass, but a breakpoint on that same console.log line sees 3 different behaviors:
somePublicRegularFunc MyClass {
someField: 123,
somePrivateArrowFunc: [Function (anonymous)],
funcRefs: [ [Function (anonymous)] ],
funcRef: [Function (anonymous)]
}
somePrivateArrowFunc MyClass {
someField: 123,
somePrivateArrowFunc: [Function (anonymous)],
funcRefs: [ [Function (anonymous)] ],
funcRef: [Function (anonymous)]
}
somePrivateArrowFunc MyClass {
someField: 123,
somePrivateArrowFunc: [Function (anonymous)],
funcRefs: [ [Function (anonymous)] ],
funcRef: [Function (anonymous)]
}
somePrivateArrowFunc MyClass {
someField: 123,
somePrivateArrowFunc: [Function (anonymous)],
funcRefs: [ [Function (anonymous)] ],
funcRef: [Function (anonymous)]
}
I got these results using Version 4.6.3 of tsc and node v16.13.0 (I also saw the same with earlier versions of both).