Last active
July 3, 2018 09:52
-
-
Save langjt/61ebef8068aa48f0aa97 to your computer and use it in GitHub Desktop.
Closure
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
var createPet = function(name) { | |
var sex; | |
return { | |
setName: function(newName) { | |
name = newName; | |
}, | |
getName: function() { | |
return name; | |
}, | |
getSex: function() { | |
return sex; | |
}, | |
setSex: function(newSex) { | |
if(typeof newSex == "string" && (newSex.toLowerCase() == "male" || newSex.toLowerCase() == "female")) { | |
sex = newSex; | |
} | |
} | |
} | |
} | |
var pet = createPet("Vivie"); | |
pet.getName(); // Vivie | |
pet.setName("Oliver"); | |
pet.setSex("male"); | |
pet.getSex(); // male | |
pet.getName(); // Oliver |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment