Share a variable from an instantiated class in Typescript

116
June 01, 2019, at 06:10 AM

I have a codebase that looks like

    //Cart.ts
    import getDataForRequest from '../getData'
    class Cart{
        private user;
        private pwd;
        private env;  // THIS IS THE MEMBER I WANT TO MAKE AVAILABLE TO OTHER PARTS OF THE APP
    constructor(user: string, pwd: string, env: string) {
        this.user = user
        this.pwd = pwd
        this.env = env
    }
    doSomething() {
        getDataForRequest()
    }
   }
// getData.ts
export getDataForRequest() {
    doSomething(env)  // <==  this has to come from that member in Cart
}

I want to make env member of the class available to other modules in the app. So when I instantiate that class as const foo = new Cart('user123', '12345', 'www.google.com') that last parameter is being accessible from the getData file too without passing arguments in doSomething().

Answer 1

Pass it in as an argument.

Change:

doSomething() {
    getDataForRequest()
}

To:

doSomething() {
    getDataForRequest(this.env)
}
READ ALSO
How to make gulp.src dynamically equal to gulp.watch folder

How to make gulp.src dynamically equal to gulp.watch folder

Before I was using static gulpsrc for a single project, but I would like to make it watch and perform tasks for multiple projects

101
Socket connection error : HPE_INVALID_CONSTANT

Socket connection error : HPE_INVALID_CONSTANT

I am not able to setup the socket connection successfullyOn debug I am getting following error

83
noble BLE module - characteristic.subscribe() hitting error

noble BLE module - characteristic.subscribe() hitting error

I'm on windows 10, using the asus BT-400

118
How to use the puppeteer js authenticate function from the codecept js?

How to use the puppeteer js authenticate function from the codecept js?

I am trying to write some tests using codeceptJsBut i am not able pass the authentication for my test application

129