Reputation: 2126
I have a large js file that I want to break into multiple namespaces.
Is this a good/bad approach vs keeping everything in one namespace?
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();
});
Upvotes: 4
Views: 503
Reputation: 30498
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.
Upvotes: 1