deemaagog
deemaagog

Reputation: 101

VS code chrome debug, why is this undefined?

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?

Upvotes: 9

Views: 2516

Answers (2)

BernardV
BernardV

Reputation: 1768

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

Upvotes: 8

devrow
devrow

Reputation: 86

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

Upvotes: -2

Related Questions