Created
March 1, 2011 00:08
-
-
Save odiak/848334 to your computer and use it in GitHub Desktop.
using closure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var c = new Counter(); | |
c.add(); // 1 | |
c.get(); // 1 | |
c.add(); // 2 | |
var n; | |
n = c + 1; // n == 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Counter = function(){ | |
var count = 0; | |
var add = function(){ | |
count += 1; | |
return getCount(); | |
}; | |
var get = function(){ | |
return getCount(); | |
}; | |
var getCount = function(){ | |
return count ? count : 0; | |
}; | |
return function(){ | |
this.add = add; | |
this.get = get; | |
this.valueOf = get; | |
}; | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment