const counter = (function () {
let count = 0;
const history = [];
return {
increment(step = 1) {
count += step;
history.push(count);
return this;
},
decrement(step = 1) {
count -= step;
history.push(count);
return this;
},
getHistory: () => history,
getCount: () => count,
};
})();
counter.increment(5).increment(3).decrement(2);
console.log(counter.getCount(), counter.getHistory().join(" -> "));
Ответ: 6 5 -> 8 -> 6
JavaScript test | #JavaScript & Max