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)?
Inheritance enables you to define a class that takes all the functionality from a parent class and allows you to add more. Using class inheritance, a class can inherit all the methods and properties of another class. Inheritance is a useful feature that allows code reusability.
JavaScript is a bit confusing for developers experienced in class-based languages (like Java or C++), as it is dynamic and does not have static types. When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype.
An inherited class is known as Sub Class or Derived Class. Sub Class inherits the properties or characteristics from Base Class, also popularly known as Super class. In the programming world, Sub Class is also referred to as a child class, whereas the superclass is referred to as parent class.
Inheriting from two classes can be done by creating a parent object as a combination of two parent prototypes. The syntax for subclassing makes it possible to do that in the declaration, since the right-hand side of the extends clause can be any expression.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With