简体   繁体   中英

How can I use the value of a previous object property inside another property of the same object when I am declaring it

I have a class which returns an object with its properties. I would like to access to the value of a previous prop inside the constructor.

I have next working code:

class KafkaConsumer {
    constructor (metaDataBrokerList, groupID, autoCommit, AutoOffsetReset, topicName) {
        return { 
            consumer: this.create(metaDataBrokerList, groupID, autoCommit, AutoOffsetReset, topicName),
            connect: (consumer) => { this.connect(consumer) }
        };
    }


    create (metaDataBrokerList, groupID, autoCommit, AutoOffsetReset, topicName) {
        var consumer = new Kafka.KafkaConsumer({
            'metadata.broker.list': metaDataBrokerList,
            'group.id': groupID,
            'enable.auto.commit': autoCommit, // don't commit my offset
            'auto.offset.reset': AutoOffsetReset, // consume from the start
        });

        .
        .
        .

        return ( consumer );
    }

    // Conect the consumer
    connect (consumer) {
        consumer.connect();
    }
}

And the call is (as you can see, I need to pass the created variable to function 'connect'. I want avoid that):

let kafkaConsumer = new KafkaConsumer(...props);
// Connect the consumer
kafkaConsumer.connect(kafkaConsumer.consumer);

I wanna something like:

class KafkaConsumer {
    constructor (metaDataBrokerList, groupID, autoCommit, AutoOffsetReset, topicName) {
        return { 
            consumer: this.create(metaDataBrokerList, groupID, autoCommit, AutoOffsetReset, topicName),
            connect: this.connect(consumer) 
            // Where 'this.connect' is my function and 'consumer' is the previous prop
        };
    }

    .
    .
    .

}

So the call should be:

// Connect the consumer
kafkaConsumer.connect();

It is a bit unclear what you are trying to achieve.

  • Why do you want to return something in the constructor?
  • Why do you need a create method that just returns an object with the parameters you have in your constructor?
  • What should the connect method do?

If you are trying to create a consumer object and return it, perhaps this solution would work for you:

class KafkaConsumer {
    constructor (metaDataBrokerList, groupID, autoCommit, AutoOffsetReset, topicName) {
        this.consumer = {
            'metadata.broker.list': metaDataBrokerList,
            'group.id': groupID,
            'enable.auto.commit': autoCommit, // don't commit my offset
            'auto.offset.reset': AutoOffsetReset, // consume from the start
        }
    }

    get() {
        return this.consumer;
    }
}

And use it like this:

let consumer = new KafkaConsumer(props);
consumer.get(); // Returns your created consumer

I have achieved my purposes. I have done that every property of the class was associated to a private function of its, so, every class property is a function which call another private function:

class KafkaConsumer {
    constructor (metaDataBrokerList, groupID, autoCommit, AutoOffsetReset, topicName) {
        this.consumer = this.p_create(metaDataBrokerList, groupID, autoCommit, AutoOffsetReset, topicName);
        this.connect = () => { this.p_connect() };
    }

    // Conect the consumer
    p_connect () {
        this.consumer.connect();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM