Live data from Hacker News

The jQuery Divide:Understand where jQuery ends and JavaScript begins

slideshare.net

11–20 of 78 posts

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#11
post #3

So, how does one become a good, non-spaghetti-code javascript developer for the web? I don't really like js so far, but there are just no alternatives in some cases. And my code ends up like those counter examples. Where can learn how to organize it properly?

> how does one become a good, non-spaghetti-code javascript developer for the web?

Just like anything else, it will take time and active effort. Till then, I suggest work with frameworks which help you structure your code. I use backbone.js and I have heard good things about YUI.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#12
post #9

Hey! That's exactly how my jQuery code looks! I think I just got seduced by all the closurey goodness that comes with Javascript (my day-job languages don't let me do that.) I'm not going to ask how to write good code, but I think this is a fair question: can someone point to a coding standard / style guideline for Javascript + jQuery? E.g., when to choose a single-use named function over an anonymous function? When…

> can someone point to a coding standard / style guideline for Javascript + jQuery?

backbone.js is good. That gives you a fair idea about structuring your application. If you webapp is designed in a RESTful manner, backbone.js maps quite nicely to your backend.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#13
post #9

Hey! That's exactly how my jQuery code looks! I think I just got seduced by all the closurey goodness that comes with Javascript (my day-job languages don't let me do that.) I'm not going to ask how to write good code, but I think this is a fair question: can someone point to a coding standard / style guideline for Javascript + jQuery? E.g., when to choose a single-use named function over an anonymous function? When…

http://www.javascriptmvc.com

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#14
post #8

I'm not so sure I buy the premise that jquery is unsuitable for large scale applications. I think it is based on an assumption that jquery should provide a framework for these things, but really - what jquery is, is a framework for the controller layer of an application. If you want to use it, you have to provide the model level framework your self. I don't think this is the fault of jquery anymore than it would be t…

jQuery as commonly used leads, in my personal experience, to hard-to-maintain programs. Just like Ruby on Rails, or Perl.

There is also anecdotal evidence that jQuery qua jQuery has some fundamental limits. I worked on a largish jQuery application, written by ninja badasses, and it creaked a lot more than it should have for its size.

Comparable systems built on YUI seemed to have more structure. I actually went through the exercise of converting a prototype from jQuery to YUI3, and spent a lot of time working out their respective "philosophies". (see http://jsrosettastone.com)

There are newer frameworks (eg JavelinJS) that aim to be to jQuery what jQuery was to other frameworks.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#15
post #9

Hey! That's exactly how my jQuery code looks! I think I just got seduced by all the closurey goodness that comes with Javascript (my day-job languages don't let me do that.) I'm not going to ask how to write good code, but I think this is a fair question: can someone point to a coding standard / style guideline for Javascript + jQuery? E.g., when to choose a single-use named function over an anonymous function? When…

I think the most important, and often violated principle is to keep model and presentation code separated. If your widget has any kind of even slightly complex logic, don't rely on the dom to be your model, or you'll get into a mess quickly. The dom (and thus jquery) is presentation (controller/view). Create separate objects for your model and have your controller code (even handlers) update this and your view be updated from it.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#16
post #3

So, how does one become a good, non-spaghetti-code javascript developer for the web? I don't really like js so far, but there are just no alternatives in some cases. And my code ends up like those counter examples. Where can learn how to organize it properly?

GWT solves it for me while javascript has these problems.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#19
Maybe the MVC design pattern doesn't work so well with jQuery in the view, acting as a controller and also for display logic.

Surely there is a design pattern that does not try to shoehorn AJAX via jQuery into what was once an MVC pattern?

MVC for webapps was around long before AJAX, yet the design pattern remained the same, after the widespread introduction of AJAX in webapps. Until very recently, there has been little traction in an 'evolved' design pattern, incorporating what the js is doing.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#20

People at my job call all JavaScript jQuery, it is so annoying. Even more annoying is that their code (jQuery or not) plain sucks

When I use jQuery (MooTools is my lib of choice) I find myself using it in ways that is very unlike the majority of jQuery that is seen around the web.

I absolutely hate the plugin system simply because you cannot easily get the instance of the plugin an refer to it later. To answer this I use function constructors with the module pattern (its easy and doesnt require an extra lib to get going) to create my plugins. Another thing that I do is when I query for a collection of objects, create an array that represents every item in that collection already wrapped with the jQuery object. This may not seem like a big deal, but there is a difference between

    var collection = jQuery('a');
    collection.each(function(i, l){
        var link = jQuery(l);
        link.bind('click', function(){//do suff});
    });
and

    var collection = jQuery('a'),
        collected = (function(){
            var c = [];
            $.each(collection, function(i, item){
                c.push(jQuery(item));
            });
            return c;
        })();
this second way allows you to select an item from collected without having to rewrap it with the jquery object. I dont have any data on it, but it seems like rerunning jQuery() with every mouseover/mouseleave/event etc seems like a waste of processing.

Anyway, that is just a few ways that I use jQuery to make javascript a bit easier. These little tricks have the people that I work with thinking that I'm some sort of genius.

Post reply on HN