I'm not much of a javascript developer right now, as I just recently found the need to learn it. I'm using jQuery, and Javascript (sometimes stupidly, like when I find out jQuery already "abstracted out" something I wrote a 20 line function for). My question is about style though, with all the anonymous functions wrapped in jQuery functions, I get lost some times looking for the missing } or ) or ; (i like to use the ; even though I think it's not needed in some places). I had to get a little open-source .js file for part of my site, and when I looked at it, the author had written all the functions to be wrapped in a kind of namespace (using a dictionary i think). It looked like this:
var myLibrary = { myFunction1 : function () {
// code for this function here
return true
},
myFunction2 : function (i) {
var s = i + 1;
return s + "px"
}
}
This allows the functions to be called like this: myLibrary.myFunction1()
I thought that was neat, so I used it for my own library. But as the file grew (again, partly because I was re-writing stuff that I really didn't have to), it got quite messy. Especially with all the indentation of jQuery calls with settings that take anonymous functions, which may have functions inside of them. So for another small project I'm working on, I adopted the straight-forward approach like this:
function myFunction1() {
//do stuff
return true
}
function myFunction2 (i) {
var s = i + 1;
return s + "px"
}
But then I have to make sure I'm not overriding any existing functions, which I think I'm not, but still.
My question is, what is the current trend? What do every-day-javascript-developers use?