Skip to content

Instantly share code, notes, and snippets.

@joshblack
Last active August 29, 2015 14:11
Show Gist options
  • Save joshblack/a5dc4b01278afe02d8b5 to your computer and use it in GitHub Desktop.
Save joshblack/a5dc4b01278afe02d8b5 to your computer and use it in GitHub Desktop.
function User(username) {
const secretPasswordKey = Symbol('password');
this.username = username;
this.setPassword = function setPassword(password) {
this[secretPasswordKey] = password;
};
this.authenticate = function authenticate(attempt) {
return attempt === this[secretPasswordKey];
};
}
let u = new User('jbfr');
u.setPassword('myPassword');
console.log(u['secretPasswordKey']); // null
console.log(u[Symbol('secretPasswordKey')]); // null
console.log(u.authenticate('badPassword')); // false
console.log(u.authenticate('myPassword')); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment