Skip to content

Instantly share code, notes, and snippets.

@nherment
Last active December 25, 2015 02:58
Show Gist options
  • Save nherment/6905953 to your computer and use it in GitHub Desktop.
Save nherment/6905953 to your computer and use it in GitHub Desktop.
private scope vs prototype
function FooPublic() {
this._publicVariable;
}
FooPublic.prototype.increment = function() {
return ++ this._publicVariable;
}
var f = new FooPublic();
f.increment(); // nice
f._publicVariable --; // now, I'm messing with the internals
function FooPrivate() {
var privateVariable = 0;
this.increment = function() {
return ++privateVariable;
}
}
var f = new FooPrivate();
f.increment(); // nice
f.privateVariable --; // f does not have member 'privateVariable'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment