Skip to content

Instantly share code, notes, and snippets.

@CrabDude
Created February 10, 2011 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrabDude/821270 to your computer and use it in GitHub Desktop.
Save CrabDude/821270 to your computer and use it in GitHub Desktop.
Pvt.js Example: Simple (Non-Inheritable) Private Instance Members
// Utilize a closure to keep the private members private
var Person = (function() {
// Generate a private instance member accessor function "pvt"
var pvt = Pvt(),
// private static member
species = "Homo sapien";
var Self = function(isDancing) {
// set private instance member
pvt(this).dancing = isDancing;
};
Self.prototype = {
// public methods
dance: function(){
// get private instance member
return pvt(this).dancing;
},
quitDancing: function() {
// set private instance member
pvt(this).dancing = false;
},
getSpecies: function() {
// get private static member
return species;
},
setSpecies: function(newSpecies) {
// set private static member
species = newSpecies;
}
};
return Self;
})();
var p = new Person(true);
// Check public member access to private members
p.dance(); // => true
p.quitDancing(); p.dance(); // => false
p.getSpecies(); // => "Homo sapipens"
p.setSpecies("Ninjus Invisibus"); p.getSpecies(); // => "Ninjus Invisibus"
// Check to see if private members are in fact private
typeof Person.species == 'undefined'; // => true
typeof Person.dancing == 'undefined'; // => true
typeof p.dancing == 'undefined'; // => true
typeof p.species == 'undefined'; // => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment