Is it a good practice to put functions inside of class objects?

I am currently making a class called MainRESTHandler and it's going to have many functions, so I wanted to categorise the many functions in various objects. For example
export class MainRESTHandler {
constructor() {
this.auth.register = this.auth.register.bind(this);
this.restaurants.location = this.restaurants.location.bind(this);
}

auth = {
register() {
// ...
}
}

restaurants = {
location() {
// ...
}
}
}
export class MainRESTHandler {
constructor() {
this.auth.register = this.auth.register.bind(this);
this.restaurants.location = this.restaurants.location.bind(this);
}

auth = {
register() {
// ...
}
}

restaurants = {
location() {
// ...
}
}
}
3 Replies
Antonio Wang
Antonio Wang2y ago
Hmm, probably isn't the best way as I'm using TypeScript and typings don't really work ideally there...I'll stick with the old way I guess
13eck
13eck2y ago
Here's the thing with classes: it automatically handles the prototype for you so all of your methods inside the class are shared among all the various instances of MainRESTHandlers. So if you have a lot of them then you'll want a class with the methods declared within so you're not duplicating code.
Antonio Wang
Antonio Wang2y ago
I see
Want results from more Discord servers?
Add your server