Skip to content

Instantly share code, notes, and snippets.

@mateuszsokola
Created July 28, 2017 17:34
Show Gist options
  • Save mateuszsokola/08a81f777f515d239903dbf4f4829e49 to your computer and use it in GitHub Desktop.
Save mateuszsokola/08a81f777f515d239903dbf4f4829e49 to your computer and use it in GitHub Desktop.
Closures

Closures

Closures are scope-bounded bindings.

When can i use them?

  • hide variables
  • hide private functions
  • factories
function counterFactory() {
var counter = 0;
return function () {
return counter += 1;
}
}
// counter is hidden within the closure
console.log('counter: ', counter); // ReferenceError: counter is not defined.
// we can use closure for creating function factories
const count = counterFactory();
console.log('count: ', count()); // 1
console.log('count: ', count()); // 2
console.log('count: ', count()); // 3
console.log('count: ', count()); // 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment