Guest User

Untitled

a guest
Feb 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. // Closures
  2. var counter = (function(){
  3. var privateCounter = 0;
  4. function changeBy(val){
  5. privateCounter += val;
  6. }
  7. return{
  8. increment: function(){
  9. changeBy(1);
  10. },
  11. decrement: function(){
  12. changeBy(-1);
  13. },
  14. value: function(){
  15. return privateCounter;
  16. }
  17. };
  18. })();
  19.  
  20. console.log(counter.value());
  21. counter.increment();
  22. counter.increment();
  23. counter.increment();
  24. console.log(counter.value());
  25. counter.decrement();
  26. console.log(counter.value());
Add Comment
Please, Sign In to add comment