Last active
December 18, 2015 17:40
-
-
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"
This file contains hidden or 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
// 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