views:

31

answers:

1

Hi I am converting some prototype javascript to jQuery javascript.

In prototype we can do Class.create to create a class. However, jQuery doesn't provide this. So I was thinking to rewrite the class into a jQuery plugin. Is this the best practice? My concern is that if I do all for the class, then I will add a lot of things to jQuery object.

The other alternative is to use some extra lines of codes I found http://ejohn.org/blog/simple-javascript-inheritance/#postcomment. Then you can do the following:

var Person = Class.extend({
  init: function(isDancing){
    this.dancing = isDancing;
  },
  dance: function(){
    return this.dancing;
  }
});

Or else, i found this jQuery plugin called HJS that allows you to create Class as well.http://plugins.jquery.com/project/HJS

However, I am not use is it necessary to use these plugins to allow me to use Class or I can just convert the class into the way to write a jQuery plugin, please advise. Thank you very much!

+1  A: 

jQuery doesn't handle JavaScript prototype inheritance.
I don't think it would be right to handle your classes using jQuery - it simply isn't meant for that. jQuery's main power are in cross-browser compatibilaty, ajax and DOM manipulation. Apart from that it offers very little utility functions, and doesn't offer any JavaScript expantions, unlike prototype.
Migration of many of this functions is therefor impossible, but you can keep using the functions you need from prototype.

Kobi
Thanks for your answer. I think you are right. jQuery plugin is not meant for that. When I was working on the migration from prototype to jQuery, I realized that prototype has more power in terms of all the methods on DOM manipulation, because they extend the native DOM element. However, I can't keep the functions from prototype. I need to convert everything to jQuery. I don't want to maintain two different frameworks. So maybe I should use the class.Extend method above to achieve something like Prototype class.create.
Shanison