Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var v=Counter();
- v();
- function Counter()
- {
- var count=0;
- this.counterIncrement = function() //used 'this' for create an object method which can access outside
- {
- count++;
- console.log(count);
- }
- //return counterIncrement; //no need to return this method,
- }
- var v = new Counter();//used 'new' keyword to create an object of 'counter'
- v.counterIncrement();//access ojbect method.
- v.counterIncrement();//out put 2
- v.counterIncrement();//out put 3
- function Counter()
- {
- var count=0;
- var counterIncrement=function()
- {
- count++;
- console.log(count);
- }
- return counterIncrement;// Here you are returning function
- }
- var v=Counter();
- //v.counterIncrement();// This will not call like that
- //Directly call
- v();
- var counter = (function() {
- var count = 0;
- return {
- counterIncrement: function() {
- count++;
- console.log(count);
- },
- };
- })();
Add Comment
Please, Sign In to add comment