Hacker News new | past | comments | ask | show | jobs | submit login
Ultra-Chaining with jQuery (ejohn.org)
18 points by qhoxie on Oct 14, 2008 | hide | past | favorite | 5 comments



My one and only gripe with jQuery is the heavy emphasis on chaining method names on top of method names. Sure, the code you get out is terse and relatively readable, but you're not coding Javascript anymore. And Javascript is too cool not to code in, given the chance. Just ask the people writing your frameworks...


But you are coding in JavaScript. You just make sure that your methods return the current object if you want them to be chainable.

The real JavaScript of jQuery doesn't happen when you're writing simple transformations on the page, instead, you really get deep into it once you start extending jQuery through plugins. To really write these correctly, you need a deep understanding of JavaScript (because you'll be reading the jQuery source code at many points).

More than anything else, jQuery is an API to make the DOM manageable (itself an API of sorts).

What's great about JavaScript in many ways is that it's not Python (which is also great in so many ways): It's very flexible and lets you write your code as you see fit. I don't think that jQuery could happen so easily and cleanly in many other languages.

But any serious object-oriented language needs to be able to method chain, and it needs to feel natural. Even PHP can do it (I used this in a template engine to generate HTML structures very quickly):

  $template->div("#header")->ul()->li()->a("A new link");


You can do the same sort of chaining in Smalltalk, it can be confusing if you don't know exactly what's being returned of course.


You can do the same sort of chaining in any language with a method call dot operator...


i had a[n ancient] professor that thought the exact same way about C and assembly.

i'd say its a gripe with any abstraction that takes you away from the lower-levels of development for any language, if its a language you love to develop in.

price of progress, and such.




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: