Hacker News new | past | comments | ask | show | jobs | submit login

"It doesn't force you to program or organize your code in a certain way"

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.



That isn't necessary. This does the same thing:

  var d = $('#elm');
  d.color('red');
  // do some stuff here.
  d.text('new text');


Some use the convention that jQuery objects should be prefixed with $, so:

  var $d = $('#elm');
  $d.color('red');
  $d.text('new text');
I do this, and like it, despite my classic aversion to both Systems Hungarian and GW-BASIC.


A few years ago I might have agreed with your statement. It was the reason why I used Prototype instead of jQuery: prototype extends the HTMLElement classes directly instead of requiring a wrapper object like jQuery does.

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.


It boils down to philosophy. Not all developers like the $() function. And it's not even specific to JQuery anymore, most libraries are doing this now. And for old school JavaScript developers, the "ninja-javascript" just doesn't sit right.


If you want to access the underlying DOM element (which I very rarely find myself needing to do when working with jQuery - the $-enhanced version provides methods for everything I might want to do with it) a shorter idiom than .get(0) is this:

    var el = $('#el')[0];




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: