Ya know, I was just playing with class inheritance the other day working on my Balrog vs. Gandalf experiment. Because the Balrog and Gandalf are both "beings" of sorts, they share a lot of common stuff (they can be falling, they have an elevation, they can die, etc). so, rather than repeating properties and methods on two different classes, I wanted to create a base class that could be extended to them both. I ran into exactly the same issues you did... and here's how I solved it. First, the file with my being class:
class being{
constructor(){
this.elevation = 1;
this.isFallingWayDown = false;
this.isSmote = false;
this.isDead = false;
}
hitBottom(){
this.isFallingWayDown = false;
}
fallWayDown(){
this.elevation--
if (this.elevation === 0){
this.hitBottom();
}
}
fall(){
this.isFallingWayDown = true;
}
die(){
this.isDead = true;
Log.Message('default death');
}
}
module.exports = {being: new being()}
Next, I created a classWizard that will instantiate Gandalf
var classBeing = require('classBeing');
var yellAttributes = Log.CreateNewAttributes();
yellAttributes.BackColor = clSilver;
yellAttributes.FontColor = clBlue;
yellAttributes.Italic = true;
var speakAttributes = Log.CreateNewAttributes();
speakAttributes.BackColor = clBlue;
speakAttributes.FontColor = clSilver;
speakAttributes.Italic = false;
var actionAttributes = Log.CreateNewAttributes();
actionAttributes.BackColor = clWhite;
actionAttributes.FontColor = clPurple;
var being = classBeing.being.constructor;
class Wizard extends being{
constructor(){
super();
}
yell(words){
Log.Message(words, '', pmNormal, yellAttributes);
}
speak(words){
Log.Message(words, '', pmNormal, speakAttributes);
}
strikeGround(groundObject, enemy){
Log.Message('--LIGHTNING SMASH!--', '', pmNormal, actionAttributes);
if (groundObject.constructor.name === 'Bridge'){
groundObject.collapse();
enemy.fall();
}
}
smiteEnemy(enemyObject){
this.speak('I threw down my enemy and smote his ruin upon the mountainside');
enemyObject.die();
}
die(){
this.isDead = true;
Log.Message('Darkness took him. And he strayed out of thought and time.', '', pmNormal, actionAttributes);
aqUtils.Delay(10000, 'Stars wheeled overhead, and every day was as long as the life age of the earth.');
}
resurrect(){
this.isDead = false;
this.speak('And I come back to you now, at the turn of the tide.');
}
kickButt(){
Log.Message('--SMASH ENEMY WITH SWORD--', '', pmNormal, actionAttributes);
}
}
module.exports.Wizard = Wizard;
The key is that the "extends" points to the constructor method of an instance of the being and not the class itself. This is how I worked around it because, in the TestComplete environment, classes and objects are "local" to the individual files so, to access them in another file, you need to return an object instance.
See if this works for you. There may be a better way, but this is how I solved inheritance