JSFiddle - React, Tailwind, and code Playground
by Farzad YZ
JavaScript
/* function createCounter() {
const counter = {
value: 0,
inc,
dec
};
function inc() {
counter.value++;
}
function dec() {
counter.value--;
}
return counter;
} */
function createCounter() {
// internal state
let value = 0;
const counter = {
getValue() {
return value;
},
inc() {
value++;
},
dec() {
value--;
}
}
return counter;
}
// c.getValue(), c.inc(), c.dec()
// ====
const c = createCounter();
console.log(c);
console.log(c.getValue());
c.inc();
console.log(c.getValue());
c.dec();
console.log(c.getValue());