Tyson Lloyd Cadenhead – User Interface Developer
June 12, 2010

There are literally tons of JavaScript frameworks out there to ease the pain of website and application development. Obviously, the most popular one out there right now is jQuery, but that is not to say that there are not other libraries out there. So the question becomes, should developers use libraries or is it just a crutch that keeps them from learning real JavaScript? Here are some thoughts on the subject.

  • I think that everyone should start out learning the basics of JavaScript before learning a library. When you see some of the things that you can do with jQuery or YUI or Prototype, you’ll probably want to jump right into the framework, but here are some benefits of learning JavaScript proper to begin with:
    • When you know the basics of JavaScript such as how to loop over an array, how to write a conditional statement and how to manipulate the DOM, you’ll have a better understanding of what the library your using is doing. Contrary to popular opinion, jQuery is not magic, and it does not do anything that you couldn’t do with plain JavaScript, it just makes it easier to do things that would previously have taken more code by running the variables you give it through functions.
    • When you know the basics, you will be able to tell when something is easier to do without the library and when the library is helpful. You won’t have to trust that it’s necessarily better just because it’s a library method. It may not be.
    • Libraries have limitations. There are things that you can do with plain JavaScript that you can’t do with a library. No JavaScript library was built to be a replacement for JavaScript, they were all written to be an addition to it.
  • Once you know the basics of JavaScript, using a framework or library will help you to write less code and do more with it. Specifically with jQuery, manipulating the DOM, applying listeners and events and performing AJAX requests requires significantly less code.
  • Frameworks and libraries extend functions that somebody else wrote, so you don’t have to. Let’s just face it, John Resig and the jQuery team could probably write loops around you with their code. They are good at what they do. The best part is that they already did it, so you don’t have to. You can focus on other things!
  • Don’t get too attached. If you can, try to abstract the way you interact with your framework in case you ever need to switch frameworks or the framework you use stops being supported. These things happen. I’ve been trying to find an article abstracting JavaScript frameworks to point you to, but I haven’t found one yet. Perhaps I will have to write one.

These are my thoughts on the subject. Can you think of any arguments for or against using JavaScript frameworks?

  1. On that last point “Don’t get too attached” – I haven’t figured out a good way to do that either. I’ve come up with a couple approaches but nothing that I really like. For me, I feel like it might be overkill because really, how often do you want or NEED to change the library you use after the fact? I’ve never needed to but I may be in the minority. Obviously i n a perfect world reducing coupling between the component parts is good but at what point is it not worthwhile?

    Comment by Dustin Martin — June 12, 2010 @ 10:02 pm

  2. Yeah, it’s definitely a tough call to make. Another drawback of it is that it increases your overall file size and slightly increases your overhead to go through another function every time you’re trying to access your library.

    I guess another good point to loosely coupling your library, though is that if there are changes in the library between updates that end up breaking your code, it’s a whole lot easier to make a change in one place than throughout your code. jQuery has gotten better about it, but there were definitely some things that would work on earlier versions that didn’t work on later versions.

    I don’t know that either abstracting your library or hitting it directly is completely perfect, but I’m definitely looking at ways to make it more loosely coupled.

    Comment by tysoncadenhead — June 13, 2010 @ 1:03 am

  3. One thing I’ve recently started doing in my applications is to not ever call the library directly from my modules. I make certain to pass it in – dependency injection. That way if I need to I can change something before the library gets passed in to my modules (like change the way a function works, add a deleted function, or create an object to map a new library’s function). It isn’t perfect though. My modules could still get DEEPLY intertwined with the library so it might be insanely difficult to change later. But this way, it is just a little better than just calling the framework from it’s global variable directly.

    Comment by Dustin Martin — June 13, 2010 @ 1:40 am

  4. Dustin, I’d really like to see how you’re setting up your applications. I’m working on a pretty comprehensive app that needs to be scalable and easy to change going forward. I’m just laying the groundwork right now, but I’m trying to stay away from JavaScriptMVC on this one so that I won’t be dependent on the jQuery library and because I don’t need view templates because I’ll be doing all of my views with EXTJS. I’ve been using a lot of concepts that Nicholas Zakas from Yahoo! has talked about in his presentations, but like I said, I’m only just beginning. Any chance I could see what you’ve been cooking up?

    Comment by tysoncadenhead — June 14, 2010 @ 6:05 pm

  5. Tyson, I’ve put my framework code up along with a very simple (and untested) snippet of code demonstrating some of the features. It really just shows the basic mechanics of how my framework functions. That said, it doesn’t really show how I architect my applications. That is something I’m still working on though I think I’ve maybe “figured it out” while using this in a current project. I’d love to discuss that further with you some time if you are up for it. And of course, if you have some suggestions, please let me know.

    You can find the code at http://gist.github.com/438615

    Let me know if you have some questions or if you want to chat about it sometime.

    Comment by Dustin Martin — June 15, 2010 @ 2:56 am

  6. Thanks Dustin! That is really helpful. I had gotten to the point where I was broadcasting events, but I was having trouble figuring out a good way to listen for the events. You’ve given me a good place to start once I get home tonight.

    Comment by tysoncadenhead — June 15, 2010 @ 2:17 pm

Leave a comment

Portfolio

See More