4 ms·
I'm rather fond of that style, or the variant: projectname = { A_CONSTANT: ..., someFunc: function(...) { }, ...
by Roboprog 13y ago
I'm rather fond of that style, or the variant:
projectname = {
A_CONSTANT: ...,
someFunc: function(...) {
},
...
lastFunc: function(...) {
}
}
Basically, JSON layout but with the functions in the resulting namespace.
Debugging & diagnostics for this layout in Firefox and Chrome is not a problem. As for IE, well, some things can't be fixed, I guess.
- sopooneo 13y agoI do that a bit, but found it tricky to end everything with a comma, and effectively use ":" instead of "=" for declarations. Lately I've been doing projectname = (function(){ self = {}; self.A_CONSTANT = ...; self.someFunc = function(...){ }; self.lastFunc = function(...){ }; return self; })(); I think that produces the exact same end result, but someone set me straight if I'm mistaken.
- Roboprog 13y agoWhat you do is a bit more complicated, but has the benefit of making your data "private" within a closure. You are using a function as an object, and assigning other functions as fields within its "prototype". I think these inner functions can call each other w/out a prefix, as methods of the same object. My version is the "static method" version that requires the namespace and a dot before any function calls. I'm OK with that, unless I need a bunch of instances of something. P.S. - the semicolons don't really do anything (other than make Doug C feel good), as JS is a line oriented language, and will complete statements when they "look done", semis or not. Think of semicolons in JS like colons in BASIC: they let you put a second statement on the same line.