JavaScript 101 – Private Variables

Funny things happen in the course of piecing together solutions in this field. Programmers have a tendency to throw their hands up in the air when they get to the end of a week and they can’t quite figure out a concept, and the problem is compounded when looking at programmers who don’t take the time to actually learn the new idioms of a language before getting into the position of writing production code.

JavaScript lends itself to this type of behavior. It has Java in it’s name, and it looks a lot like C or Java syntax, so it attracts a lot of OO type behavior. Little do these people know the language should be written more like lisp and less like Java.

One of the first important concepts that is shared with OO programmers is encapsulation – the art of hiding internal components from those who don’t need to be aware of them. It is a very common practice in C++, Java and C#, so you should be aware of it. JavaScript has it’s own flavor of this, but it comes in a very different form.

public class MyClass
{
    private string _myPrivateString;
    public string MyPublicString;

    public MyClass()
    {
        _myPrivateString = "test";
        MyPublicString = "test";
    }
}

[...] // Somewhere else in your code...

var MyObject = new MyClass();

The analogous construct in Javascript is very different, for a number of reasons.

var MyObject = function()
{
    var myPrivateString = "test"
    this.myPublicString = "test";
}

I am not telling you this to get you to start using closures to try to emulate your object oriented tendencies, but to highlight something fun. If you open some browsers and console.log an object of this form you will find that it outputs the object, including an _private element with the internals of that object attached to it.

To the untrained eye, this looks like a way to access private variables.

To the skilled eye, it is just the browser providing a means for you to audit the internals of your code.

The following is an excerpt from the public API that I am responsible for maintaining at work…

var net;
net = net || {};
net.rnmd = net.rnmd || {};
net.rnmd.sdk = net.rnmd.sdk || {};
net.rnmd.sdk._private = net.rnmd.sdk._private || {};

net.rnmd.sdk._private.loadingImg = "";
net.rnmd.sdk._private.closeImg = "";
net.rnmd.sdk._private.overlayPlayImg = "";
net.rnmd.sdk._private.overlayPauseImg = "";
net.rnmd.sdk._private.haveAdData = false;
net.rnmd.sdk._private.isMobileWebFSA = false;
//Always illegal...
net.rnmd.sdk._private.callbacks = {};//[]

Just to be clear about what is going on here, a global object is being defined with a “private” object being defined that is then set, publicly, to a number of strings and values. This is in fact not private, and when you output this on any browser it will show up in the same format as any other public variable.

By the way, I bought a new horse today, and I’m calling it “a million dollars” so now i can say i am a millionaire. I have a million dollars. Don’t let the gubment know.