Last active
October 12, 2015 22:33
-
-
Save michaelficarra/d50c71ae76c258f748a2 to your computer and use it in GitHub Desktop.
pseudo-private methods using a capabilities-based approach
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
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; | |
})(); |
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
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