Created
September 11, 2017 18:48
-
-
Save Chalarangelo/d4eb611125364c72e1fdcb17fda5bb08 to your computer and use it in GitHub Desktop.
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 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