Created
February 15, 2013 19:48
-
-
Save Sljubura/4962934 to your computer and use it in GitHub Desktop.
'Private' properties.
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
// Implement private members with closure | |
function Person() { | |
var name = 'Frank'; | |
this.getName = function () { // Privileged method, has access to private property. | |
return name; | |
} | |
} | |
var user = new Person(); | |
console.log(user.name); // undefined | |
console.log(user.getName()); // 'Frank' | |
// Can be exposed: | |
var guest = new Person(), | |
nick = guest.getName(); | |
console.log(nick.name = 'Dennis'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment