为什么此JS代码中的“ this”未定义日志?

时间:2019-12-23 17:05:40

标签: javascript this

代码如下:

class Counter {
  state = {
    count: 1,
  };

  formatCount() {
    console.log(this);
    console.log(this.state.count);
  }
}

let c1 = new Counter();
c1.formatCount();

现在,我不明白为什么formatCount()不记录该对象,而状态对象中count的值为1却记录未定义。有人可以告诉我为什么它记录的是未定义的,我该怎么解决?谢谢。

我在chrome控制台上尝试过,这就是我得到的:

output

0 个答案:

没有答案