0

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?

3
Contributors
4
Replies
37
Views
4 Years
Discussion Span
Last Post by Troy III
0

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

There's no current trend. Each project varies.

0

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 ?

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.