I've been searching around Stack Overflow, and the web in general, for a decent explanation of something I'm seeing in some legacy JavaScript. So far I haven't had much luck, so I've decided to take the extreme measure of actually posting a question. :-)
The code isn't super old, but it pre-dates my involvement in the project and (of course) the developer who originally created it left before I got here.
So here we go:
Normally when I'm looking @ code that creates JavaScript 'classes' I see something like:
var SomeClass = function() { ..stuff.. }
...other code...
var objSomeClass = new SomeClass();
Also familiar is the JavaScript literal:
var someLiteral = { ..stuff.. }
...other code...
var someResult = someLiteral.someFunction();
What I'm seeing in the legacy code appears to be a combination of the two styles, and I've never seen anyone do this before. So what we've got is:
var someLiteral = { ..stuff.. }
...other code...
var objSomeLiteral = new someLiteral();
Also, in the same code are declarations like this:
function doStuff() { ..stuff.. }
...other code...
var objDoStuff = new doStuff();
Again, I've never seen anyone write code quite like this before.
Is this semantically incorrect code, or is there some valid reason for doing it this way that I'm just not aware of? Which is always possible, I make no claims to JavaScript expertise at this point.
The code definitely works, but if it's not 'good' code then I'm going to want to re-write it. Cuz I'm just anal like that. :-)
Thanks!