D
Daniel Norden
Hello.
Can you give me some feedback on my code layout?
I have everything organized in an object, "NRL", that's used as a namespace,
and there are other subobjects/modules below that, for example "dialog"
or "ajax". I'm interested in general comments, and I have some specific
questions, but first the (sample) code:
var NRL = {};
......
NRL.ajax = {};
......
NRL.ajax.ListControl = function (arg1, arg2) {
// first, some local "constants" (configuration)
var PAGER_BUFFER = 5,
EXPERIMENTAL_SORTING = false,
.....;
// next, the public members
this.paused = false;
this.currFoo = "";
.....
// public methods next
this.setTrigger = function (src) {
.....
};
this.fetchRows = function (argX, argY) {
.....
};
///// "private" after this line /////////////////////
var node, header, .....,
.....
spinner = NRL.ajax.Spinner.getInstance(),
that = this;
// private functions only accessible in/below this scope
function analyzeHeader () {
.....
}
function receiveUpdate (jsonData) {
.....
}
......
///// some initializations (not in all objects) //////////
node = document.createElement("div");
.....
};
It works well, as far as I can see, and I like the separation of "public"
and "private" areas in the source. When other people read the code, they
can immediately see where the public "interface" ends. Some questions:
1) I'm a little worried about the many closures... does that use up a huge
amount of memory? If so how can I improve the layout? Some of the functions
have other inner functions as well, like when I add event handlers or
callbacks for XMLHttpRequest, that also act as closures.
2) I have seen people using function expressions instead of function
statements for the "private" functions. Do function expressions have any
advantage in this case?
3) Most of the objects don't ever need it, but what would be the best way to
let other objects inherit from a structure like this?
4) Some of these objects are singletons, like the NRL.ajax.Spinner in the
example above. I currently handle them like this:
NRL.ajax.Spinner = function () {
..... same as above .....
};
NRL.ajax.Spinner.getInstance = function () {
var instance;
return function () {
instance = instance || new NRL.ajax.Spinner();
return instance;
}();
};
Now somebody could still use "new NRL.ajax.Spinner()" directly, but that
could be avoided by adding a check to the Spinner constructor. I don't want
to create an object literal for the singleton unless an object (instance)
is really required, and besides the constructor could have initialization
code, like creating elements.
Thanks everybody,
Dano
Can you give me some feedback on my code layout?
I have everything organized in an object, "NRL", that's used as a namespace,
and there are other subobjects/modules below that, for example "dialog"
or "ajax". I'm interested in general comments, and I have some specific
questions, but first the (sample) code:
var NRL = {};
......
NRL.ajax = {};
......
NRL.ajax.ListControl = function (arg1, arg2) {
// first, some local "constants" (configuration)
var PAGER_BUFFER = 5,
EXPERIMENTAL_SORTING = false,
.....;
// next, the public members
this.paused = false;
this.currFoo = "";
.....
// public methods next
this.setTrigger = function (src) {
.....
};
this.fetchRows = function (argX, argY) {
.....
};
///// "private" after this line /////////////////////
var node, header, .....,
.....
spinner = NRL.ajax.Spinner.getInstance(),
that = this;
// private functions only accessible in/below this scope
function analyzeHeader () {
.....
}
function receiveUpdate (jsonData) {
.....
}
......
///// some initializations (not in all objects) //////////
node = document.createElement("div");
.....
};
It works well, as far as I can see, and I like the separation of "public"
and "private" areas in the source. When other people read the code, they
can immediately see where the public "interface" ends. Some questions:
1) I'm a little worried about the many closures... does that use up a huge
amount of memory? If so how can I improve the layout? Some of the functions
have other inner functions as well, like when I add event handlers or
callbacks for XMLHttpRequest, that also act as closures.
2) I have seen people using function expressions instead of function
statements for the "private" functions. Do function expressions have any
advantage in this case?
3) Most of the objects don't ever need it, but what would be the best way to
let other objects inherit from a structure like this?
4) Some of these objects are singletons, like the NRL.ajax.Spinner in the
example above. I currently handle them like this:
NRL.ajax.Spinner = function () {
..... same as above .....
};
NRL.ajax.Spinner.getInstance = function () {
var instance;
return function () {
instance = instance || new NRL.ajax.Spinner();
return instance;
}();
};
Now somebody could still use "new NRL.ajax.Spinner()" directly, but that
could be avoided by adding a check to the Spinner constructor. I don't want
to create an object literal for the singleton unless an object (instance)
is really required, and besides the constructor could have initialization
code, like creating elements.
Thanks everybody,
Dano