JavaScript Name Spacing

Feeds

RSS Feed

<< January | February | March >>

Wednesday, 3rd February 2010

This is just a quick article to demonstrate a quick bit of JavaScript name-spacing.

Why would you do this? Well, this allows you to put functions and variables inside of an identifier. It acts like a "box of stuff" and prevents variable and function name conflicts. It also supplies a neat way to organise and access your code in logical blocks.

If you're interested, here's how you do it.

var MyNamespace = (function() {
    var myPrivateVariable = 'Private Variable';
    
    var myPrivateFunction = function () {
        return 'Private Function';
    }
    
    return {
        myPublicVariable: 'Public Variable',
        sayHello: function (name) {
            alert('Hello ' + name + '!');
        },
        sayGoodbye: function (name) {
            alert('Goodbye ' + name + '!');
        },
        publicFunction: function () {
            alert(this.myPublicVariable + ' ' + 
                myPrivateFunction() + ' ' + 
                myPrivateVariable)
        }
    };
}());

MyNamespace.sayHello('Steve');
MyNamespace.sayGoodbye('John');
MyNamespace.publicFunction();

Obviously, in this example it's all a bit basic, but if you had JavaScript functions that you'd like to group together, this is a great way to do it - not a genuine name-space, but some distance towards organising your code.

You Are Here: Home » Blog » JavaScript Name Spacing