Skip to content

Instantly share code, notes, and snippets.

@amacdougall
Last active December 18, 2015 10:29
Show Gist options
  • Save amacdougall/5768895 to your computer and use it in GitHub Desktop.
Save amacdougall/5768895 to your computer and use it in GitHub Desktop.
JS module example.
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