Node.js access parameter from parent class from within child class method

11 Views Asked by At

I have a base class, lets name it class A which has a method, lets called it "test". This test method takes a parameter which I want to access from within the class, that inherits from class A.

Class A test(parameter)

Class B test() console.log(parameter) -> undefined.

How is the correct way of doing this?

here is my code example

class Strategy {
    constructor(name) {
        this.orderManager = new OrderManager();
        this.config = { NAME: name };
        this.loadConfigurationVariables();
    }

    loadConfigurationVariables() {
        console.info(`loading configuration variables for strategy ${this.config.NAME}`);
        try {
            for (let i = 0; i < REQUIRED_VARIABLES.length; i++) {
                this.config[REQUIRED_VARIABLES[i]] = process.env[this.config.NAME + '_' + REQUIRED_VARIABLES[i]];
            }
            if (Object.values(this.config).some((el) => el === undefined)) {
                throw new Error('not all required configuration variables set');
            }
            console.info(`Strategy ${this.config.NAME} configuration loaded`, this.config);
        } catch (error) {
            console.error(`Strategy ${this.config.NAME} loadConfigurationVariables`, error.message);
        }
    }

    async execute(symbol) {
        try {
            console.info(`executing strategy ${this.config.NAME}`);
        } catch (error) {
            console.error(`error executing strategy ${this.name}`, error.message);
        }
    }
}

class Redmonkey extends Strategy {
    execute() {
        super.execute();
        console.log(`executing ${this.config.NAME} symbol: ${symbol}`); //symbol is undefined
    }
}

Do I have to add all parameters from the parent method to all the childs I will be creating that use this function?

0

There are 0 best solutions below