Skip to content

Instantly share code, notes, and snippets.

@edysegura
Created August 14, 2015 14:22
Show Gist options
  • Save edysegura/9b1a408ba8273d47a8b9 to your computer and use it in GitHub Desktop.
Save edysegura/9b1a408ba8273d47a8b9 to your computer and use it in GitHub Desktop.
[JS] How to create private members in JavaScript
var counter = (function(){
var i = 0;
return {
get: function(){
return i;
},
set: function( val ){
i = val;
},
increment: function() {
return ++i;
}
};
})();
counter.get(); // 0
counter.set( 3 );
counter.increment(); // 4
counter.increment(); // 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment