Last active
December 18, 2015 10:29
-
-
Save amacdougall/5768895 to your computer and use it in GitHub Desktop.
JS module example.
This file contains 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
PP.component = (function() { | |
var currentValue = null; | |
var previousValues = []; | |
/** | |
* Changes input values, maintaining a record of how often it has done so. | |
* This is a standard doc comment, and this is how it is phrased; when in | |
* doubt, write JavaDoc style. | |
*/ | |
var component = { | |
getValue: function() { | |
return currentValue; | |
}, | |
setValue: function(value) { | |
previousValues.push(value); | |
this.currentValue = value; | |
}, | |
undo: function() { | |
this.currentValue = previousValues.pop() || null; | |
} | |
}; | |
return component; | |
})(); | |
component.getValue(); // null | |
component.setValue(100); // value is now 100 | |
component.setValue(200); // value is now 200 | |
component.undo(); // value is now 100 again | |
component.undo(); // value is now null again | |
// At no point have the actual variables been exposed. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment