Wednesday, April 16, 2008

JQuery VS Prototype

I have been using prototype in all my previous Rails projects but switched to JQuery recently and i found it much better than prototype. The difference is not only in the code size but also in the approach they have for the same problem. Lets explore this with an example. Lets consider a simple fade effect that you wanna produce on a DOM element.

Prototype way - Effect.Fade('someId', {duration: 0.5});

Prototype uses classes that encapsulate some functionality, we just need to pass the ids and other optional parameters and the class does what it is supposed to do.

JQuery way - $("someId").fadeOut(1000);

JQuery instead sees all the html nodes as objects to pass messages to. And when you pass a message to an object it returns you a JQuery object to which you can further pass some messages..... sounds more like the oops way....Its pretty easy to operate on list of elements on your page using JQuery.
And also once you are comfortable with the JQuery syntax you can easily use Hpricot test extension to test your Rails views.