Last active
August 29, 2015 13:57
-
-
Save cjmyles/9479576 to your computer and use it in GitHub Desktop.
Private Members in Javascript
This file contains 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
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