Namespacing technique in JavaScript, recommended? performant? issues to be aware of?

Posted by Bjartr on Stack Overflow See other posts from Stack Overflow or by Bjartr
Published on 2010-01-20T15:39:15Z Indexed on 2010/06/10 23:32 UTC
Read the original article Hit count: 152

Filed under:
|
|
|

In a project I am working on I am structuring my code as follows

MyLib = {
    AField:0,

    ASubNamespace:{
        AnotherField:"value",

        AClass:function(param) {
            this.classField = param;

            this.classFunction = function(){
                // stuff
            }
        }
    },

    AnotherClass:function(param) {
        this.classField = param;

        this.classFunction = function(){
            // stuff
        }
    }
}

and so on like that to do stuff like:

var anInstance = new MyLib.ASubNamespace.AClass("A parameter.");

Is this the right way to go about achieving namespacing? Are there performance hits, and if so, how drastic? Do performance degradations stack as I nest deeper? Are there any other issues I should be aware of when using this structure?

I care about every little bit of performance because it's a library for realtime graphics, so I'm taking any overhead very seriously.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about advice