Get Programming with JavaScript - Listing 11.10
Listing 11.10 - A Counter constructor
JS Bin
var Counter = function () {
var counter = 0;
this.count = function () {
counter = counter + 1;
return counter;
};
};
var peaks = new Counter();
var climbs = new Counter();
Further Adventures
Listing 11.10 - A Counter constructor - Task 2
- Add a countDown method to the Counter constructor. The method should decrease the counter by 1 each time it is called.
var Counter = function () {
var counter = 0;
this.count = function () {
counter = counter + 1;
return counter;
};
this.countDown = function () {
counter = counter - 1;
return counter;
};
};
var peaks = new Counter();
var climbs = new Counter();
Listing 11.10 - A Counter constructor - Task 3
- Add a countUpBy method that counts up by a specified amont.
var Counter = function () {
var counter = 0;
this.count = function () {
counter = counter + 1;
return counter;
};
this.countDown = function () {
counter = counter - 1;
return counter;
};
this.countUpBy = function (amount) {
counter = counter + amount;
return counter;
};
};
var peaks = new Counter();
var climbs = new Counter();