D
D
Dvorak2016-10-31 20:02:34
JavaScript
Dvorak, 2016-10-31 20:02:34

Is it ok to extend a standard object?

I am writing a telegram bot. I have an app object whose methods allow us to receive information from the user and send it. The code looks like this:

app.on('message', msg => {
  const text = msg.text;
  switch(text) {
    case '/add':
      add(redis, mongo, message, app, users);
    break:

    case '/remove':
      add(redis, mongo, message, app, users);
    break:
  }
})

And so for each team. Since in most cases the same arguments are passed to me when calling each function, I decided to extend the app object:
app.redis = redis;
app.mongo = mongo;

app.add = (){...};

Is it normal to do this in terms of readability and code quality? If not, what is the best way to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-10-31
@allishappy

If not, what is the best way to do it?

It is better to use some kind of IoC-container, there are quite a lot of them for js. This allows you to reduce code cohesion, which is always a good thing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question