tags:

views:

2082

answers:

2
var A=function(){
};

$.extend(A.prototype, {
 init:function(){
  alert('A init');
 }
});
var B=function(){

};

$.extend(B.prototype,A.prototype,{
 init:function(){
  alert('B init');
 }
});
var p=new A();
p.init();
var x=new B();
x.init();

is the above the best way to create class and inheritance in jQuery? In B's init how do I invoke parent's init (similar to super.init() in OO languages)?

+5  A: 

For OO, it's best to look outside jQuery. jQuery is based on collections returned by selectors.

If you want classes, some choices are Base2, Joose, and JS.Class.

Nosredna
+1  A: 

John Resig created a snippet for simple inheritance here. http://ejohn.org/blog/simple-javascript-inheritance/

he save the super class to a _super variable so you can

this._super();

you can reference his code snippet to get a better idea of what he another helpful post is: http://alexsexton.com/?p=51

dspiteself