Created
September 2, 2011 14:06
-
-
Save user24/1188669 to your computer and use it in GitHub Desktop.
Revealing Module Pattern
This file contains hidden or 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
// Define an object using the Revealing Module Pattern | |
var myCounter = function() { | |
// Both Private and Public methods and properties are defined here | |
var counter = 0; | |
function count() { | |
return 0+counter; | |
} | |
function increment() { | |
counter++; | |
} | |
function decrement() { | |
counter--; | |
} | |
// Return references to those members which you wish to be public. | |
return { | |
'increment':increment, | |
'decrement':decrement, | |
'count':count | |
}; | |
}(); | |
// Use the public interface. | |
myCounter.count(); // 0 | |
myCounter.increment(); | |
myCounter.count(); // 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment