views:

104

answers:

2

I am having trouble writing code JavaScript code. I work with ASP.Net MVC and send a lot of data back and forth for Ajaxy features. The problem is that this code is becoming hard to manage and messy:

  • I need to do similar functions but can't find a decent way to reference methods (such as if they are in different files).
  • Methods do not wait until others complete which a design problem I have not encounted before.

It's a mess. Any recommendations?

+2  A: 

Javascript is pretty tricky as its pretty easy in the beginning and you can write pretty cool stuff without knowing that much about the language. But then at the same time its very hard to master it (by no means do I master javascript). I would recommend you check out this site. They have some relay good videos about javascript and other front end stuff. I have learned a lot from there.

If you have lots of javascript you should probably try to find a good design pattern that fits you. I guess you are already using some kind of javascript framework, like jquery? If not, you should. For javascript intensive sites I find prototype very suitable. Its a more object oriented framework that I like a lot when you have to handle a lot of javascript.

The problem you describe with methods not waiting for each other happens because you are doing something async, like a ajax call. This can be avoided by using a callback function. Something like this:

function methodThatDoesSomethingAsync(callback) {
    //Do something async like ajax call

    //You use the callback when you have the data you want to return
    if(callback) {
        callback(data);
    }
}

methodThatDoesSomethingAsync(function(data) {
    //Do what you want with the data
});
Mattias Jakobsson
I do use jQuery otherwise I would have gone crazy already. How does Prototype compare?
Damien
Ya, I figured :) It depends on what you are doing. In my opinion jquery is better at handling the dom and its easier to get started with. I think jquery is very good when you need to build small to medium apps. But when you need to build complex stuff it becomes more important to have a good structure. In this case I think prototype is the better framework. It has more OO features that will help you a lot in structuring your code. I would take this into consideration when choosing what framework to use.
Mattias Jakobsson
Underscore.js (http://documentcloud.github.com/underscore/) is another library that adds a lot of the OO and utility features that Prototype adds, but does it without extending the native objects. It should play nicer with other libraries and avoid method collisions.
jkyle
jQuery seems good for functionality but not for structure. I am not sure if jQuery adds can OO at all. So Prototype will allow a concept of classes and objects? I can structure my code in such a way that allows me to call functions of objects from other files?
Damien
You can, of course, work in a OO manner with jquery as well. But you will have to do all that work your self as jquery doesn't help you anything there. Prototype will allow you to create classes and work with them much like in a modern server side OO language. I have not tested Underscore.js as jkyle mentioned so I can't tell you much about that. But from the looks of it, it seem like a OO framework as well that will probably integrate better with jquery. You should also check out scriptaculous (http://script.aculo.us/). Thats pretty much like jquery ui for prototype.
Mattias Jakobsson
Thanks! I'll look into that. Especially the OO help because I need some structure there.
Damien
+2  A: 

JavaScript: The Good Parts is a must-read primer by the Granddaddy of JavaScript.

JavaScript: The Definitive Guide is the best detailed reference guide that separates DOM and JavaScript-as-a-language.

Object-Oriented JavaScript will tell you a dozen different ways to handle your objects and methods in JavaScript.

I've read and enjoyed them all.

jkyle