VS code chrome debug,为什么这个未定义?

dee*_*gog 5 javascript visual-studio-code

我正在使用 VS 代码和 chrome 调试器扩展。下面的代码执行没有错误并产生预期的结果,但是我看到 WATCH 部分中的“this”未定义。

class Q {
    constructor() { 
        this.arr = [1,2,3]
    }
    log(e) {
        console.log(e)
    }
    test() {
        this.arr.forEach(e => {
            this.log(e); // this is undefined when debugging
        })
    }
}

const f = new Q().test()
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Ber*_*rdV 5

为避免与thisJavaScript 关键字冲突,TypeScript在转译时重命名this_this。试试看吧_this

  • 要回答我自己的问题,请在引用“_this”之前添加“let _this = this;” (2认同)

小智 -2

class Q {

  arr = []

  constructor() { 
     this.arr = [1,2,3]
  }
  log(e) {
     console.log(e)
  }
  test() {
     this.arr.forEach(e => {
         this.log(e);
     })
  }
}

const f = new Q().test()
Run Code Online (Sandbox Code Playgroud)