Skip to content

Instantly share code, notes, and snippets.

@dgowrie
Last active December 18, 2015 17:40
Show Gist options
  • Save dgowrie/7defc835032f1f2c702a to your computer and use it in GitHub Desktop.
Save dgowrie/7defc835032f1f2c702a to your computer and use it in GitHub Desktop.
Public / private members on a "class" via the Pseudoclassical pattern using Prototypal Inheritance combined with the Revealing Module pattern - AKA a "revealing prototype pattern"
// constructor
function Person(name) {
this.name = name;
// etc... but no methods here
}
// prototype for all methods, using an IIFE with a returned object for 'public' methods
Person.prototype = (function() {
// private properties and methods as needed
// the "_" convention helps identify the members as "private"
var _skillLevel = 0;
var _privateMethod = function() {
// do private stuff
};
// public methods
var publicMethod = function() {
// do public stuff
};
var increaseSkillLevel = function() {
_skillLevel++;
};
var getSkillLevel = function() {
return _skillLevel;
};
// expose public members via returned object, as in revealing module pattern
return {
publicMethod: publicMethod,
increaseSkillLevel: increaseSkillLevel,
getSkillLevel: getSkillLevel
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment