Last active
August 29, 2015 14:11
-
-
Save heathkit/9fcedb5c5a23ec6a0e8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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