开发者

What are the current differences between jQuery and Prototype? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 7 years ago.

Improve this question

I've been building a Ruby on Rails site in recent months and I've only used a small amount of built in JavaScript functions. However I will be doing much more JavaScript development in the coming weeks and months and I am debating on which JavaScript framework to go with.

On the one hand jQuery seems to be the more popular one, but then again Prototype is already built into Rails. I've also read a bunch of articles on开发者_如何学Cline from a few years ago talking about how jQuery is more concise at some things but sloppy on others, and giving various other opinions.

So my questions are to the people who have used both (preferably recently):

  • What is the difference in using either Prototype and jQuery from a pure JavaScript and from a Ruby on Rails perspective?
  • Is there a significant difference between them or are they now pretty close to each other in terms of functionality and code writing?
  • How high is the switching cost in terms of things that have to be relearnt and code that has to be rewritten?

Thanks


Previous discussion

Much, if not all of your question has been discussed already. See the search or:

  • How would you go about for switching a site from Prototype to jQuery
  • Prototype, dojo or jQuery?
  • Why does everyone like jQuery more than prototype/script.aclo.us or mootools or whatever?
  • Is it true that Jquery + Rails is problematic?
  • Using jquery in Rails

Switching from Prototype to JQuery

I'm in the process of switching from Protoype to JQuery, mainly for reasons of performance (I have seen too many benchmarks with Prototype always coming in last to JQuery, MooTools and Dojo.) I would say switching cost is not horrible, because most basic concepts (Selection, Effects, Ajax) are very similar. However, every line of Prototype code has to be carefully rewritten - carefully especially because many helper functions and constructs ($ $$) look deceivingly similar.

If your code contains many complex chained constructs that may be available in Jquery as well, but function differently, migration can become a very cumbersome task. If you use it only for quick $() element selection, effects and a bit of Ajax, not so much.

Either way, be prepared for a phase of intense learning. JQuery constructs are small and smart, but are terrible to read IMHO (and look god-awful compared to a block of native Javascript, but that's a different discussion). It definitely takes time to get familiar with the syntax and principles if you're coming from a different framework, or native Javascript.


Pekka's answer is great, but one thing that should be added is that Rails is moving heavily in the direction of being Javascript framework agnostic. Although when Rails first came out, its AJAX integration was innovative, the Javascript landscape has changed dramatically since then, and the Rails integration doesn't really live up to best practices anymore. As such, the Prototype integration should not be considered a major factor today on Rails 2.3, and it will be even less of a factor in Rails 3.0 which will likely be released within the next few months. I recently read an interesting article describing some of the most significant changes to Rails' Javascript integration in years.

My current project has thousands of lines of legacy Prototype code, but we've recently started using jQuery because its benefits were too great to pass up. My impressions:

Prototype is a mature framework that does a very good job shoring up browser differences and providing very well-rounded functionality. Unfortunately it is a bit heavy-handed with its changes to the default Javascript environment, and it's not seeing rapid development and doesn't have a significant plugin ecosystem.

jQuery is a newer framework that was able to leverage some of the lessons of the early javascript frameworks like Prototype to create something more modular, less obtrusive, and significantly more powerful and concise. I still love Prototype, but jQuery's DOM manipulation and plugin ecosystem is just too good to ignore anymore. If I was starting a greenfield project I'd have to go with jQuery because that seems to be where the innovation is happening (at least between those two choices).


There is a pretty big difference to the way you work with jQuery compared to prototype, once you get used to the nuances of the frameworks it becomes pretty easy to switch between either of them.

The biggest hurdle in the beginning is the fact that Prototype hooks itself into the dom right away. As soon as you include it into the page all dom elements you look up will have the prototype functionality attached to them. With jQuery it leaves all the elements alone until you access one with the jQuery selector $('css-class'). The object you get back from that call will have the jQuery methods attached.

If you want to ease yourself into jQuery you could leave everything using prototype and include jQuery and call jQuery.noConflict(); http://docs.jquery.com/Core/jQuery.noConflict. This will give the '$' method back to prototype and let you use jQuery by calling jQuery('css-class') instead.

If you want to fully jump into jQuery I would recommend using the jRails plugin (http://github.com/aaronchi/jrails). It will replace all the rails ajax helpers and have them use jQuery instead of prototype. I've done this in a number of projects and have never had any problems.

In the end you should just take a look at prototype and jQuery and see which one fits your coding style. You can accomplish the same things with both frameworks, it's just a slightly different syntax and way of thinking for each.


jQuery is more efficient for small, smart client effects. prototypejs is more efficient for deep javascript development.

jQuery has a host of plugins for robust UI effects. prototypejs has a complete library for Enumerables, Functions, Hashes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜