Skip to content

Instantly share code, notes, and snippets.

@oliverwebr
Created June 12, 2020 12:00
Show Gist options
  • Save oliverwebr/3aec248761a3ba6ebd5a5c4a33e45b9a to your computer and use it in GitHub Desktop.
Save oliverwebr/3aec248761a3ba6ebd5a5c4a33e45b9a to your computer and use it in GitHub Desktop.
JS-Closures Example-1: Privat Values
const createCounter = () => {
let _privatCounter = 0;
const increment = () => {
_privatCounter += 1;
};
const decrement = () => {
_privatCounter -= 1;
};
const getCounter = () => _privatCounter;
return {
increment,
decrement,
getCounter,
};
};
const counter = createCounter();
counter.increment();
counter.increment();
console.log(counter.getCounter());
console.log(counter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment