Javascript encourages this:
function Counter(init) {
this.count = init || 0;
this.inc = function() { return ++this.count; };
this.dec = function() { return --this.count; };
}
let c = new Counter(10);
console.log(c.dec());
console.log(c.dec());
But Javascript classes are a disgusting mess full of quirks. So just don't use them. Instead do this:
function newCounter(init) {
let counter = {};
counter.count = init || 0;
counter.inc = function() { return ++counter.count; };
counter.dec = function() { return --counter.count; };
return counter;
}
let c = newCounter(10);
console.log(c.dec());
console.log(c.dec());
No class crap here, just simple code.
[–]x0x7 1 insightful - 3 fun1 insightful - 2 fun2 insightful - 2 fun2 insightful - 3 fun - (0 children)
[–]BanditMcFuklebuck 1 insightful - 3 fun1 insightful - 2 fun2 insightful - 2 fun2 insightful - 3 fun - (3 children)
[–]IMissPorn 2 insightful - 2 fun2 insightful - 1 fun3 insightful - 1 fun3 insightful - 2 fun - (2 children)
[–]fschmidt[S] 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 0 fun2 insightful - 1 fun - (1 child)
[–]IMissPorn 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 0 fun2 insightful - 1 fun - (0 children)
[–]jingles 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 0 fun2 insightful - 1 fun - (0 children)
[–]package 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 0 fun2 insightful - 1 fun - (8 children)
[–]fschmidt[S] 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 0 fun2 insightful - 1 fun - (7 children)
[–]package 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 0 fun2 insightful - 1 fun - (6 children)
[–]fschmidt[S] 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 0 fun2 insightful - 1 fun - (5 children)
[–]package 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 0 fun2 insightful - 1 fun - (4 children)
[–]fschmidt[S] 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 0 fun2 insightful - 1 fun - (3 children)
[–]package 2 insightful - 2 fun2 insightful - 1 fun3 insightful - 1 fun3 insightful - 2 fun - (2 children)
[–][deleted] (1 child)
[removed]
[–]Vulptex[A] 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 0 fun2 insightful - 1 fun - (0 children)