使用此vs对象引用

时间:2017-02-16 07:48:21

标签: javascript

我想知道为什么我不能在第14行使用setTimeout(this.updateState, 1000);。当我使用setTimeout(v.updateState, 1000)时,代码可以正常工作。

var  GREEN = 1, YELLO= 2, RED = 3;
var v = {
  state: GREEN,
  updateState: function(){

    if(this.state == GREEN)
      this.state = YELLO;    
    else if(this.state == RED)
      this.state = GREEN;
    else
      this.state = RED;

    console.log(this.state);
    setTimeout(this.updateState, 1000); // this line works when I have setTimeout(v.updateState, 1000); wondering why this.updateState does not work
  }
};

setTimeout(v.updateState, 1000);

0 个答案:

没有答案