Reputation: 173
I got the following problem :
1) Two classes in class.js
Class1 {
//here the constructor
constructor(connection){
this.connection = connection
}
//here a class1 prop
returnConnection(){
return this.connection
}
}
// class 2
Class2 extends Class1 {
getClass1ConstructorConnection{
return super.returnConnection();
}
}
module.exports = {Class1, Class2}
Now in my route file I declare class1
var { Class1 } = require(./class.js);
var { Class2 } = require(./class.js);
var class1Object = new Class1({connectionObj})
And after that, i declare class2
var class2Object = new Class2();
if I try to console.log class2 prop like this
console.log(class2Object.getClass1ConstructorConnection())
It gets undefined.
Now, I know the best would be to declare the connectionObj as a native constructor of class 2, but for a bunch of reasons I can't, so, is there a way to inherit a constructor variable value by a parent class after its declaration in the main app workflow?
Thank you for your help!
Upvotes: 0
Views: 1494
Reputation: 1847
Because you never pass the connection to the base class. You have to pass your connection to the constructor
var class2Object = new Class2({connectionObj});
Upvotes: 1