Guest User

Untitled

a guest
Jan 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. var v=Counter();
  2. v();
  3.  
  4. function Counter()
  5. {
  6. var count=0;
  7. this.counterIncrement = function() //used 'this' for create an object method which can access outside
  8. {
  9. count++;
  10. console.log(count);
  11. }
  12. //return counterIncrement; //no need to return this method,
  13.  
  14. }
  15.  
  16. var v = new Counter();//used 'new' keyword to create an object of 'counter'
  17. v.counterIncrement();//access ojbect method.
  18.  
  19. v.counterIncrement();//out put 2
  20. v.counterIncrement();//out put 3
  21.  
  22. function Counter()
  23. {
  24. var count=0;
  25. var counterIncrement=function()
  26. {
  27. count++;
  28. console.log(count);
  29. }
  30. return counterIncrement;// Here you are returning function
  31. }
  32.  
  33. var v=Counter();
  34. //v.counterIncrement();// This will not call like that
  35. //Directly call
  36. v();
  37.  
  38. var counter = (function() {
  39. var count = 0;
  40. return {
  41. counterIncrement: function() {
  42. count++;
  43. console.log(count);
  44. },
  45. };
  46. })();
Add Comment
Please, Sign In to add comment