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?

Member Avatar for LastMitch

My question is, what is the current trend? What do every-day-javascript-developers use?

There's no current trend. Each project varies.

What do you mean by:

But then I have to make sure I'm not overriding any existing functions, which I think I'm not, but still.

?

Why would that happen ?

It's a slim chance, but in other languages it is fairly easy to accidently override builtin functions (python comes to mind). Functions with simple or common names like "encode", "inspect", or otherwise may conflict. It's not just JavaScript builtins though, it's also any library I might be using (jQuery and other libraries try to use $ at the same time). Without truly knowing JavaScript yet, there is a small chance that I could override the default functionality without knowing it. Again, this is only because of my lack of knowledge when it comes to JavaScript. I was wondering if JS developers use those namespaces for any reason except preventing conflicts, or what style is more common with JS developers.

why is it that it never happens to stick a fork in your eye while you eat?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.