最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VS code chrome debug, why is this undefined? - Stack Overflow

programmeradmin3浏览0评论

I`m using VS code and chrome debugger extension . The code below is executed without errors and produces the expected result, however i see that 'this' is undefined in WATCH section.

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()

what am I doing wrong?

I`m using VS code and chrome debugger extension . The code below is executed without errors and produces the expected result, however i see that 'this' is undefined in WATCH section.

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()

what am I doing wrong?

Share Improve this question edited Aug 26, 2019 at 21:24 Bergi 667k161 gold badges1k silver badges1.5k bronze badges asked Feb 10, 2018 at 11:13 deemaagogdeemaagog 1012 silver badges5 bronze badges 2
  • Sounds like a Watch limitation rather than anything truly concerning. Does the code actually work? – Lightness Races in Orbit Commented Feb 10, 2018 at 16:13
  • Possible duplicate of Value of "this" is incorrect when debugging Babel transpiled React with Chrome Devtools – Bergi Commented Aug 26, 2019 at 21:24
Add a ment  | 

2 Answers 2

Reset to default 8

To avoid conflicts with the this JavaScript keyword, TypeScript renames this into _this when transpiled. Try watching for _this instead.

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()
发布评论

评论列表(0)

  1. 暂无评论