Created
June 16, 2018 00:23
-
-
Save ldco2016/1a4146c25557e45f02327261f6ced645 to your computer and use it in GitHub Desktop.
example of Closure
This file contains hidden or 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
// Creates an object that can be decremented or incremented. | |
// Note that value can never be changed without using | |
// increment or decrement. This illustrates a use of closure | |
// the increment, decrement, getValue functions have access | |
// to value since functions inherit their outer scope. | |
// | |
// But note that there's no way to modify value except by | |
// using the increment and decrement functions. Thus, | |
// closures can be used to enforce privacy. | |
function getCounter(value) { | |
return { | |
increment: function() { value++ }, | |
decrement: function() { value--; }, | |
getValue: function() { return value; } | |
}; | |
} |
Author
ldco2016
commented
Jun 16, 2018
for (var i = start; i < end; i++) {
// do something
}
var coinFace = Math.floor(Math.random() * 2);
while(coinFace === 0){
console.log("Heads! Flipping again...");
var coinFace = Math.floor(Math.random() * 2);
}
console.log("Tails! Done flipping.");
var understand = true;
while( ){
console.log("I'm learning while loops!");
understand = false;
}
var bool = true;
while(bool === true){
console.log("Less is more!");
bool = false;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment