Text

Jul 28, 2010
@ 2:06 pm
Permalink

Attempt #1 at JS Namespaces

Trying to see how well I can tie up my brain into a knot;

var Main = Main || {};

(function ()
{
    function NS() {
        var privateVar = "";

        this.publicVar = "";

        this.setter = function(str) {
            privateVar = str;
        }

        this.getter = function() {
            alert(privateVar);
        }
    }

    Main.NS = new NS();
})();

var o = Main;

alert(o.NS.publicVar);
o.NS.publicVar = "this is a public var";
alert(o.NS.publicVar);

o.NS.getter();
o.NS.setter("setting the private var");
o.NS.getter();

Now let’s see how well I can shoot myself in the foot…