Javascript - Module Pattern via function

Introduction

A module is a function with inner variables and functions.

It can then be exposed to an outside by returning a "public API,".

The public API gives access to the data inside the function via methods that have closures over the data.

function Message(text) { 
    function printMessage() { 
        console.log("This is the message: " + text + "!"); 
    } 
    return { 
        printMessage: printMessage 
    }; 
} 

var printer = Message("test"); 
printer.printMessage();          // This is the message: test! 

Here, the variable printer is assigned a function Message with an argument "test."

Message is the function implementing the module pattern.

It gives a printer a public API with access to its inner function printMessage as a method that prints out the text parameter.