I had some pseudoclasses that shared a large part of their initialization. I decided to take this initialization out and create a base class, from which they’ll inherit.
function BaseClass(param1, param2) {
...
}
function SubClassA(param1) {
...
}
function SubClassB(param1) {
...
}
I want SubClass1 and SubClass2 to inherit from BaseClass in the following manner:
SubClassA(param1) constructor calls BaseClass(param1, "I am A.")
SubClassB(param1) constructor calls BaseClass(param1, "I am B.")
so BaseClass adds some properties to them. Then both subclasses do some initialization of their own.
Now I can’t just do SubClassA.prototype = new BaseClass(), because I want the super constructor to take parameters. How to do this elegantly?
When you do a
new SubClassA(param1)ornew SubClassB(param1)base constructor will be called with appropriate parameters.Also, there are other ways than
SubClassA.prototype = new BaseClass()to define base class. You can check this question for some details. (Disclaimer: The question was asked by me.)