Douglas Crockford’s Javascript Pattern

I keep forgetting this and having to look for it, so I’m recording it here for easy future access:

myModule = function () {

    var myPrivateVar;
    var myPrivateMethod = function () { }

    return  {
        myPublicProperty: "something",
        myPublicMethod: function () { }
    };
}();

1 Comment so far

  1. Edward O'Connor on July 26th, 2007

    Thanks for the reminder! I just told Emacs to auto-populate new .js files with this.

Leave a Reply