Skip to content

Instantly share code, notes, and snippets.

@ldco2016
Created June 16, 2018 00:23
Show Gist options
  • Save ldco2016/1a4146c25557e45f02327261f6ced645 to your computer and use it in GitHub Desktop.
Save ldco2016/1a4146c25557e45f02327261f6ced645 to your computer and use it in GitHub Desktop.
example of Closure
// 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; }
};
}
@ldco2016
Copy link
Author

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