问题
Here is an example:
function main () {
return () => {
console.log(42);
};
}
main()();
If we put a breakpoint on the line with console log statement and inspect the value of this on the Scope panel in DevTools, we can see that:
However, if we add this as a second argument to the logging statement, we can see a bit different picture:
One always assumes this is borrowed by an arrow function from the outer lexical environment.
Since we run the code in the non-strict mode the value for this for main function should always be equal to Window.
There are similar questions (this one and that one) but they both describe the issue with a transpiled code while in our case it is pure JavaScript.
Hence the logical question - is this is an issue with DevTools or it is by design?
来源:https://stackoverflow.com/questions/54590233/chrome-debugger-shows-non-obvious-incorrect-value-for-this-inside-an-arrow-fu