Created
February 25, 2024 13:50
-
-
Save hongphuc5497/83298600cc11338972452faa54f4a3a2 to your computer and use it in GitHub Desktop.
Module Design Pattern in JS
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
// Module using the Module Design Pattern | |
var Module = (function() { | |
// Private variable | |
var privateVariable = "I am a private variable"; | |
// Private function | |
var privateFunction = function privateFunction() { | |
console.log("This is a private function"); | |
} | |
// Public function | |
var publicFunction = function() { | |
privateFunction(); | |
console.log("This is a public function"); | |
} | |
// Public interface | |
return { | |
publicFunction:publicFunction, | |
// Public variable accessing private variable | |
publicVariable: privateVariable, | |
}; | |
})(); | |
Module.publicFunction() | |
console.log(Module.publicVariable) | |
console.log(Module.privateVariable) | |
Module.privateFunction() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment