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

function multiplier(factor) {
    return number => number * factor;
}
function multiplier(factor) {
    return function(number) { return number * factor };
}

@ldco2016
Copy link
Author

for (var i = start; i < end; i++) {
  // do something
}

@ldco2016
Copy link
Author

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.");

@ldco2016
Copy link
Author

var understand = true;

while(  ){
	console.log("I'm learning while loops!");
	understand = false;
}

@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