en.javascript.info/1-js/06-advanced-functions/03-closure/4-counter-object-independent/task.md
Ilya Kantor 964ed57c42 closure
2019-12-26 14:59:57 +03:00

417 B

importance: 5


Counter object

Here a counter object is made with the help of the constructor function.

Will it work? What will it show?

function Counter() {
  let count = 0;

  this.up = function() {
    return ++count;
  };
  this.down = function() {
    return --count;
  };
}

let counter = new Counter();

alert( counter.up() ); // ?
alert( counter.up() ); // ?
alert( counter.down() ); // ?