views:

38

answers:

1

I have a large js file that I want to break into multiple namespaces.

  1. Is this a good/bad approach vs keeping everything in one namespace?

  2. In NAMESPACE_FIRST how do I call map build?

    var NAMESPACE_FIRST = {
        init:function() {
            alert("onload functions");
        },
        this.map:function() {
            this.length = 0;
        },
        this.map.prototype.build:function(){
            return this.length;
        }
    };
    
    
    var NAMESPACE_SECOND = {
        upload:function() {
              //do something
        },
        delete:function() {
              //do something
        }
    };
    
    
    $(function () {
        NAMESPACE_FIRST.init();
    });
    
+1  A: 

Nice question!

Firstly, your JavaScript is a little erroneous. To achieve the effect I think you want, your first namespace should look like:

var NAMESPACE_FIRST = {

    init: function() {
        alert("onload functions");
    },
    map: function() {
        this.length = 0;

        NAMESPACE_FIRST.map.prototype.build = function() {
            alert("1");
            return this.length;
        }
    }
}

So, answering question 2, your onready will be something like:

$(function () {
    NAMESPACE_FIRST.init();

    var obj = new NAMESPACE_FIRST.map();
    obj.build();
}

Regarding question 1, I've no real opinion either way.

James Wiseman