0

Why variable doesn`t increase?

function Ticker() {
  this._i = 0
}

Ticker.prototype = {
  tick: function() {
    console.log(this._i++);
  }
}

var ticker = new Ticker();
setInterval(ticker.tick, 1000);

and what _ means?

abc123
  • 16,261
  • 6
  • 46
  • 76
timur
  • 5
  • 1
  • 4
    setTimout alters this....`console.log(this._i, this)` – epascarello Nov 16 '18 at 16:15
  • What if you do `setInterval(ticker.tick.bind(ticker), 1000);`? – Frank Modica Nov 16 '18 at 16:15
  • The other comments have addressed why `ticker._i` is not increasing as you would expect. As for what the `_` means - it doesn't "mean" anything, it's a valid character to use in a variable or property name. But by convention, properties which are meant to be "private" (not directly accessed/altered) are often given names beginning with an underscore. – Robin Zigmond Nov 16 '18 at 16:17

0 Answers0