Well, if you're using prototype inheritance to create new classes, you'll do something like this:
function MyBaseClass() {
// common stuff here
}
function MySubClass() {
// subclass-specific stuff here
}
MySubClass.prototype = new MyBaseClass();
That last line is required to establish the inheritance chain. However, it also has the side-effect of executing the body of MyBaseClass
, which might cause problems (particularly if the MyBaseClass
function is expecting arguments).
If you don't want that to happen, do something like this:
function MyBaseClass() {
this.init = function() {
// initialisation stuff here
}
// common stuff here
}
function MySubClass() {
// subclass-specific stuff here
this.init();
}
MySubClass.prototype = new MyBaseClass();
The initialisation code in init
is now only executed when you create an instance of MySubClass
.