V
V
vetsmen2018-02-12 10:12:32
JavaScript
vetsmen, 2018-02-12 10:12:32

Is the constructor correct?

I'm trying to make a constructor that I will create in a loop a certain number of times to work with different parameters, I did something like this:

const setUserMagager = (config) => {
  init: () => {
    this.client = new ClientUser();
    this.clientManager = new ClientManager({
      user: this.client
    });

    this.options = {
      name: config.name,
      key: config.key
    };

    this.cookies = [];

    client.login(options);
    this.viewer();
  },
  viewer: () => {
    this.client.on('login', () => {
      this.client.setLogin();
    });

    this.client.on('msg', (session) => {
    	serveSession(session);
    });

    this.clientManager.on('user', (data) => {
      serveNewUser(data);
    });
  }
}

Interested in the adequacy of the above when. Particularly confusing moments in the form of calling the viewer function from init, as well as on () handlers in the viewer (when receiving any data, they will have to be called), will this work at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-02-12
@vetsmen

I don't understand the purpose of this entity, but it's not a constructor. The viewer method is named incorrectly because the name doesn't say anything about what the method does.
A constructor is when:

spoiler
function SetUserManager(config) {
  this.client = new ClientUser();
  this.clientManager = new ClientManager({
    user: this.client
  });

  this.options = {
    name: config.name,
    key: config.key
  };

  this.cookies = [];

  client.login(options);
  this.viewer();
}

SetUserMagager.prototype.viewer = function() {
  this.client.on('login', () => {
    this.client.setLogin();
  });

  this.client.on('msg', (session) => {
    serveSession(session);
  });

  this.clientManager.on('user', (data) => {
    serveNewUser(data);
  });
}

or like this:
spoiler
class SetUserMagager {
  constructor(config) {
    this.client = new ClientUser();
    this.clientManager = new ClientManager({
      user: this.client
    });

    this.options = {
      name: config.name,
      key: config.key
    };

    this.cookies = [];

    client.login(options);
    this.viewer();
  }
  
  viewer() {
    this.client.on('login', () => {
      this.client.setLogin();
    });

    this.client.on('msg', (session) => {
    	serveSession(session);
    });

    this.clientManager.on('user', (data) => {
      serveNewUser(data);
    });
  }
}

One gets the impression that you are trying to use OOP everywhere and produce entities where they are not needed and where it is better to use the functional style.
You can’t say anything about your handlers, since it’s not clear what ClientUser is and what ClientUserManager is .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question