Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Last active October 12, 2015 22:33
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 michaelficarra/d50c71ae76c258f748a2 to your computer and use it in GitHub Desktop.
Save michaelficarra/d50c71ae76c258f748a2 to your computer and use it in GitHub Desktop.
pseudo-private methods using a capabilities-based approach
var ClassName = (function() {
var PRIVATE = {};
function ClassName(state) {
this.state = state;
}
ClassName.prototype.publicMethod = function publicMethod() {
this.privateMethod(PRIVATE, "...");
};
ClassName.prototype.privateMethod = function privateMethod(secret, otherArg) {
if (secret !== PRIVATE) throw new Error("private method called without private capability");
console.log(this.state);
console.log(otherArg);
};
return ClassName;
})();
const ClassName = (() => {
const PRIVATE = {};
return class ClassName {
constructor(state) {
this.state = state;
}
publicMethod() {
this.privateMethod(PRIVATE, "...");
}
privateMethod(secret, otherArg) {
if (secret !== PRIVATE) throw new Error("private method called without private capability");
console.log(this.state);
console.log(otherArg);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment