Chrome debugger shows non-obvious (incorrect?) value for this inside an arrow function

偶尔善良 提交于 2020-01-06 08:49:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!