Skip to content

Instantly share code, notes, and snippets.

@Sljubura
Created February 15, 2013 19:48
Show Gist options
  • Save Sljubura/4962934 to your computer and use it in GitHub Desktop.
Save Sljubura/4962934 to your computer and use it in GitHub Desktop.
'Private' properties.
// 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