JavaScript constructors inside a namespace

Posted by Joe on Stack Overflow See other posts from Stack Overflow or by Joe
Published on 2012-06-10T16:28:24Z Indexed on 2012/06/10 16:40 UTC
Read the original article Hit count: 183

Filed under:
|

I have read that creating a namespace for JavaScript projects helps to reduce conflicts with other libraries. I have some code with a lot of different types of objects for which I have defined constructor functions. Is it good practice to put these inside the namespace as well?

For example:

var shapes = {
    Rectangle: function(w, h) {
        this.width = w;
        this.height = h;
    }
};

which can be called via:

var square = new shapes.Rectangle(10,10);

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about namespaces