Taslim Oseni
Taslim Oseni

Reputation: 6263

Nodejs: Referencing constructor variables from instance variables

I have a bit of an OOP issue in Javascript (Node).

I created a class called Bubble. This class has a constructor that takes in a token. Now, here's the problem:

class Bubble {
    
    constructor(token) {
        if (!token) throw new Error('No token');
        this.token = token;
    }

    first = new First(this.token)
    second = new Second(this.token)
    third = new Third(this.token)

If I perform new Bubble("someToken").first, a null token is passed. However, if I change first, second and third to functions and I perform new Bubble("someToken").first(), the exact token is passed successfully. I want to reference first, second and third as direct variables, not functions i.e new Bubble("someToken").first.

How do I go about this?

Upvotes: 1

Views: 65

Answers (2)

jmrah
jmrah

Reputation: 6222

One option is to implement your class methods as getters. Here's how that might look like in your example:

class Bubble {
    
    constructor(token) {
        if (!token) throw new Error('No token');
        this.token = token;
    }
    
    get first() {
      return new First(this.token);
    }
    
    get second() {
      return new Second(this.token);
    }

    get third() {
      return new Third(this.token);
    }   
}

With those getters in place, accessing them would look like a regular property access for the caller.

const bubble = new Bubble("token");
bubble.first
bubble.second
bubble.third

Upvotes: 3

georg
georg

Reputation: 214949

Your code uses public class fields, which are still not a part of the standard, although supported almost everywhere. The problem is, field initalizers are executed before the constructor, no matter where they are textually located:

class A {
    field1 = console.log('field initializer 1')

    constructor() {
        console.log('constructor')
    }

    field2 = console.log('field initializer 2')
}

new A

Therefore, in first = new First(this.token), the token is not defined yet. A workaround is to convert class fields into "normal" properties, initialized in the constructor:

class Bubble {
    constructor(token) {
        if (!token) throw new Error('No token');
        this.token = token;
        this.first = new First(this.token)
        ....etc
    }

Upvotes: 3

Related Questions