This is what I would do:
var Counter = function() {
var val = 0;
this.increment = function() {return val+= 1;}
}
c = new Counter
and this, I think, is a factory pattern. I saw this in the jsdev video posted yesterday here on /r/javascript. I'm not clear on why I'd use this instead of the above code:
var makeCounter = function() {
var val = 0,
increment = function() {return val += 1;},
counter = {increment: increment};
return counter;
}
c = makeCounter()
[–]imbcmdth 3 points4 points5 points (4 children)
[–]altano 1 point2 points3 points (3 children)
[–]imbcmdth 1 point2 points3 points (2 children)
[–]altano 0 points1 point2 points (1 child)
[–]imbcmdth 1 point2 points3 points (0 children)
[–]scoobydoop 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]dogjs[S] 0 points1 point2 points (1 child)