Skip to content

Instantly share code, notes, and snippets.

@cjmyles
Last active August 29, 2015 13:57
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 cjmyles/9479576 to your computer and use it in GitHub Desktop.
Save cjmyles/9479576 to your computer and use it in GitHub Desktop.
Private Members in Javascript
function Test() {
var _secret = "12345";
// private method
function __private() {
return _secret;
}
// publicly accessible method and can access private vars & methods
this.priviledged = function() {
return __private();
}
}
Test.prototype.niceTry = function() {
return this.hasOwnProperty("__private") ? this.__private : "nice try!";
}
var test = new Test();
console.log(test.priviledged());
console.log(test.niceTry());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment