Skip to content

Instantly share code, notes, and snippets.

@heathkit
Last active August 29, 2015 14:11
Show Gist options
  • Save heathkit/9fcedb5c5a23ec6a0e8a to your computer and use it in GitHub Desktop.
Save heathkit/9fcedb5c5a23ec6a0e8a to your computer and use it in GitHub Desktop.
var lm = {
buildDefaultAccumulator: function() {
return new lm.Accumulator(0);
},
version: '0.1'
};
lm.Accumulator = function(initial) {
this.value = initial;
// Sequence is a private variable - it's inaccessible outside this function's scope
var sequence = [initial];
this.accumulate = function(additional) {
this.value += additional;
sequence.push(additional);
};
this.getSequence = function() {
return sequence;
};
}
lm.Printer = function(acc) {
// acc is also like a private member variable.
this.printAccumulator = function() {
console.log(acc.value);
}
this.printSequence = function() {
console.log(acc.getSequence());
}
}
(function() {
var acc = lm.buildDefaultAccumulator();
acc.accumulate(5);
acc.accumulate(2);
var printer = new lm.Printer(acc);
printer.printAccumulator();
printer.printSequence();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment