by Encosia · 16y ago · 0 comments
Even so, you should be using a real debugger anyway at this point, not printf debugging. Stepping through tight code like that and observing some watches is very easy in a client-side debugger.
Any time you need to access DOM elements or do anything rudimentary JQuery just gets in the way because you can't even make use of local variables and for-loops without resulting in spaghetti code.
Yes it does. JQuery calls it "new wave JavaScript", but really it's just an anti-pattern. JQuery forces you to select all elements using $() before being able to use any of the methods. And you have to call $().get(0) to get the selected element out. It is this unnecessary process that gets very tiresome. Let's see if this makes it more clear:
$('#elm').color('red');
/* do some stuff here */
$('#elm').text('new text');
To simplify that (and not require a redundante DOM call) you'd use a local variable to store a reference to the element: var d = $('#elm').get(0);
$(d).color('red');
/* do some stuff here */
$(d).text('new text');
I argue that this is unnecessary when you could just write conventional code instead: var d = document.getElmentById('elm');
d.style.color = 'red';
/* do some stuff here */
d.innerHTML = 'new text';
The way the $() function operates just doesn't sit well with all coders, regardless of how solid JQuery is in other respects. var d = $('#elm');
d.color('red');
// do some stuff here.
d.text('new text');But for various reasons I've switched to jQuery half a year ago, and I've never looked back. I can't say that the $() stuff is that much of an issue: it's clearly documented, easy to use, and I find the general DOM methods to be so useless that I'm only using the Prototype/jQuery methods 90% of the time anyway.
Your claim that it's an anti-pattern is way exaggerated.
var el = $('#el')[0];I know that I should, and if you know a "real" debugger that works under IE7 (and preferably IE6) that is more powerful than print debugging I'd love to hear about it.