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?
No alternatives? Coffeescript is always an alternative to js: http://jashkenas.github.com/coffee-script/
The jQuery Divide:Understand where jQuery ends and JavaScript begins
31–40 of 78 posts
Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins
#32Earlier quoted context omitted.
if you need to do stuff with a inside of the function, then you have to do $(this), so every time an a in that collection is clicked $() is run. In my first example I actually took that problem out of the equation by looping through the collection and storing the jQuery'd a as a var accessible via the event closure. However, if you want to reference an element in that collection later in your code, you have to jump t…
But wouldn't multiple potentially long loops on initial load have a greater possible impact on user experience than one extra call to $() per event?
var memd = {},
getMem = function(obj){
var o;
if(memd[obj]){
o = memd[obj];
}else{
o = memd[obj] = $(obj);
}
return o;
};
and call that function instead of calling $() while inside of an event handler (I havent tested this code, but the concept is straight forward)Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins
#33At that time IMO the other libraries apart from YUI, did not seem to have everything in place to learn how to use them quickly. I was put off YUI at the time because it seemed incredibly verbose, this has since been improved.
Also I think a distinction can be made between using jQuery and using the widget factory which does allow for more modular maintainable code.
Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins
#34Earlier quoted context omitted.
But wouldn't multiple potentially long loops on initial load have a greater possible impact on user experience than one extra call to $() per event?
Great point. I think that my simplistic example is just that, simplistic. A better solution for huge collections would be to memoize the retrieval of the $() obj with something like var memd = {}, getMem = function(obj){ var o; if(memd[obj]){ o = memd[obj]; }else{ o = memd[obj] = $(obj); } return o; }; and call that function instead of calling $() while inside of an event handler (I havent tested this code, but the c…
Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins
#35I'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 throu…
People using jQuery improperly is not something wrong with jQuery. jQuery is perfectly fine for large web applications, you just need to organize your code properly, with something like a MVC and ORM layer.
Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins
#36About time someone came out and said this, good job.
He implied there was a problem, showed some code, but never showed where the problem lies or what it was, and certainly never mentioned any solution.
All I got was some vague "this isn't pretty" vibes.
Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins
#37Earlier quoted context omitted.
Great point. I think that my simplistic example is just that, simplistic. A better solution for huge collections would be to memoize the retrieval of the $() obj with something like var memd = {}, getMem = function(obj){ var o; if(memd[obj]){ o = memd[obj]; }else{ o = memd[obj] = $(obj); } return o; }; and call that function instead of calling $() while inside of an event handler (I havent tested this code, but the c…
I don't want to appear snarky, but as you admit yourself ("I dont have any data on it") you don't know if creating a jQuery object is expensive or not (to be fair, I don't know either) - so why all the extra complexity if you don't know whether the thing you are trying to avoid (creating instance of jQuery during event handlers) actually causes problems or not?
$('.class').doSomehting();
$('.class').doSomethingElse();
$('.class').thirdThing();
in favor of var ele = $('.class');
ele.doSomething();
ele.doSomethingElse();
ele.thirdThing();
I would only assume that re-querying with the jQuery object inside of event handlers has the same adverse effects.Lets say you have a very simple tabbed thing
//this is code that i've seen around
var tabs = $('a.tab'),
containers = $('div.container');
tabs.click(function(e){
var index = tabs.indexOf($(this));
tabs.removeClass('active');
$(this).addClass('active');
containers.css('display', 'none');
$(containers[index]).css('display', 'block');
});
vs
//this is how id handle a simple tabber
var tabs = $('a.tab'),
containers = $('div.container'),
all_containers = $.map(containers, function(i, c){
return $(c);
}); $.each(all_tabs, function(i, tab){
var t = $(tab);
t.bind('click', function(e){
tabs.removeClass('active');
t.addClass('active');
containers.css('display', 'none');
all_containers[i].css('display', 'block');
});
});
I just feel that the first one, while more concise (has it obvious areas of improvement, but general idea) would wind up being more expensive than the second.If running the jQuery object isnt too expensive, why is the practice of rerunning the same selector frowned upon?
Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins
#38So, 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
#39Earlier quoted context omitted.
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 throu…
> jQuery as commonly used leads, in my personal experience, to hard-to-maintain programs. People using jQuery improperly is not something wrong with jQuery. jQuery is perfectly fine for large web applications, you just need to organize your code properly, with something like a MVC and ORM layer.
I'm also very skeptical of the dozen-odd undocumented "extensions" to the selector language. They are useful, but tend to tie you to jQueryisms. I didn't realize for a long time that :first was not valid CSS3.
Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins
#40I'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 throu…
I worked on a largish jQuery application,
written by ninja badasses
I don't think those "ninja badasses" were as good as you're describing them.If they were, they would realize that jQuery is not a framework that structures your application, but just a neat and light library that provides syntactic sugar for DOM-manipulation + dealing with cross-browser issues.
jQuery as commonly used leads, in my personal
experience, to hard-to-maintain programs. Just
like Ruby on Rails, or Perl.
Well, I could say the same thing about Java/C# or PHP ... in fact, the shit I had to put up with in those (more structured) platforms is far worse than anything I have seen thus far that was done on top of jQuery / Rails and Perl. And I thought Python/Django had immunity from such problems, until a month ago.Newsflash: bad developers write bad code.
And yes, I've worked with Perl -- Catalyst and DBIx::Class are pretty good, you should check them out ;-)