views:

2010

answers:

4

We recently built a web app using Prototype, making a fair amount of use of its Class.Create() functionality. Right now, we're considering migrating to jQuery as it seems to be considered a 'better' library. I've been doing some reading on the topic and found out that jQuery does not have straightforward built-in support for class-creation like Prototype does. Of course, there's still the .prototype and the jQuery.extend (as mentioned in http://stackoverflow.com/questions/79477/how-to-create-an-object-oriented-class-in-jquery), but after some more searching, I got the feeling that jQuery doesn't really 'want' you to script this way...

The same page has a post mentioning John Resig's Classy plugin, which looked great to me. However, the page states that it was actually an April Fools Day joke, and that Resig actually advises not to use the code. I'm happy to oblige, but I'd like to understand what the reason for all this is, and, of course, what it is they DO want you to do instead. Can someone enlighten me?

-edit-

Thanks for the interesting answers. For the sake of clarity: does this mean it is fine to rewrite the classes I made in Prototype to the jQuery way of 'shaping' a class, and still use them in the same 'manner' as before? Or is this still considered bad practice, and should I use a different approach?

+9  A: 

First crappy Google search for "object oriented javascript" turned this up...but it explains the point I wanted to get across:

JavaScript Object-Oriented Programming

Just take a look at the first paragraph and you find:

Javascript isn't a fully Object Oriented programming language. The use of classes in JS is based on the prototype functionality. The Prototype library masks the use of the prototype functionality to create your class. jQuery expects you to use the prototype functionality the same way you would using regular JS to extend jQuery itself and create your own custom objects.

Justin Niessner
jQuery enhances JavaScript and does not try to simulate some other language. +1
Boldewyn
The paragraph quoted is simply wrong. Javascript is a fully Object Oriented programming language. What it isn't is a Class-based OO language. The designers of Prototype decided to provide some Class-fu to make a lot of people more comfortable (including me, when I started with it). Having read Crockford's "Javascript: The Good Parts", I now think that this was a mistake.
Colin Fine
A: 

I had exactly the same problem with converting from Prototype to jQuery, and I can still only think to say that yeah, it seems they're not all that into oop in the Prototype sense. still, coding jQuery you'll find yourself relying heavily on JSON, which i guess is the pseudo-official closest thing to oop you'll get.

some of the jQuery ui stuff actually iterates over all the properties of a parameter you pass, and does stuff with it, which means you can't do, say, Object.prototype at all, because that'll add stuff to every object, that the jQuery ui will try to treat as a relevant parameter.

speed and scalability is amazing, though, so i'd say it's a matter of time before you get over Prototype, and cometo terms with the fact that Justin's reply actually makes a valid point :)

David Hedlund
+3  A: 

jQuery for JavaScript programmers explains how jQuery encourages good OOP techniques.

karim79
A: 

jQuery does not have straightforward built-in support for class-creation like Prototype does

As it shouldn't. Class creation is best left to the programmer, not the library. I started with the Prototype library and moved away, because it was so bloated. OOP in JavaScript is not hard at all. As long as you know how to use prototypes you'll be fine.

Making the equivalent of private members in JavaScript becomes tricky when dealing with Prototype's Class.create. And honestly, I don't think Class.create gives you anything useful, that you can't do easily yourself or with jQuery.

You can always use both libraries at the same time to help with the transition. jQuery has this nice noConflict feature that returns the $ function to its original owner.

geowa4