Getting this as undefined when using arrow function

大城市里の小女人 提交于 2019-12-25 03:34:18

问题


I'm using arrow functions and I'm debugging with Chrome and Firefox Dev Tool. I am getting, this as undefined, even though the code still works.

My assumption is, that it has something to do with source-maps.

Here are the tools I use in order to build the my code:

  • webpack (devtool: eval)
  • babel-loader (es5 preset)
  • typescript-loader

回答1:


The problem is that the chrome debugger believes that the this in the source code refers to the run-time this, but this inside a arrow function in typescript source code is actually transformed to _this, so it's showing you the wrong object.

This is why it's only a problem in the debugger and the code still works fine. When I need to debug something where this is a problem, I just copy it to the console and prepend it with an underscore.



来源:https://stackoverflow.com/questions/52555422/chrome-debugger-shows-incorrect-scope-for-this-keyword

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