Skip to content

Instantly share code, notes, and snippets.

@Chalarangelo
Created September 11, 2017 18:48
Show Gist options
  • Save Chalarangelo/d4eb611125364c72e1fdcb17fda5bb08 to your computer and use it in GitHub Desktop.
Save Chalarangelo/d4eb611125364c72e1fdcb17fda5bb08 to your computer and use it in GitHub Desktop.
function user(username, password){
// Here we use closures to define public functions
// that can access private variables.
return {
getUsername() { return username; },
setUsername(newUsername) {username = newUsername; },
checkPassword(givenPassword) { return password == givenPassword; }
}
}
var myUser = user("Me", 1234);
console.log(myUser.getUsername()); // Me
myUser.setUsername("NotMe");
console.log(myUser.getUsername()); // NotMe
console.log(myUser.checkPassword(1000)); // false
console.log(myUser.checkPassword(1234)); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment