0

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!

2
  • What's connectionObj here new Class1({connectionObj})? Commented Sep 21, 2017 at 8:49
  • It's a connectionObj from mysql! And i need it as in other props of class2 i should use this connection object to do some queries... Commented Sep 21, 2017 at 8:59

1 Answer 1

1

Because you never pass the connection to the base class. You have to pass your connection to the constructor

var class2Object = new Class2({connectionObj});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.