The JavaScript Module Pattern

One thing that really bothers me is often how obtuse programmers explain relatively simple things. Take for example the JavaScript Module Pattern.

<script type="text/javascript">
    var module = (function () {
        var foo = 0;
        return {
            bar:function () {
                foo = foo + 1;

This is really just two things:

  1. An anonymous function which allows for immediate execution.
  2. Returning a function, which behave like “public variables/methods”, keeping the other functions/properties inside the module which behave like “private variables/methods”

That’s it, no need to explain this with 5 pages of explanation about encapsulation, etc.