Javascript module pattern
A handy javascript pattern from Christian Heilmann (http://www.wait-till-i.com/index.php?p=476):
var revealingModulePattern = function(){
var privateVar = 1;
function privateFunction(){
alert('private');
};
var publicVar = 2;
function publicFunction(){
anotherPublicFunction();
};
function anotherPublicFunction(){
privateFunction();
};
function getCurrentState(){
return 2;
};
// reveal all things private by assigning public pointers
return {
init:publicFunction,
count:publicVar,
increase:anotherPublicFunction,
current:getCurrentState()
}
}();
alert(revealingModulePattern.current) // => 2
revealingModulePattern.init();

0 Comments:
Post a Comment
<< Home