Created
August 14, 2015 14:22
-
-
Save edysegura/9b1a408ba8273d47a8b9 to your computer and use it in GitHub Desktop.
[JS] How to create private members in JavaScript
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
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