Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ES6 classes OR Object Literals in controllers for an Express + NodeJS app

There are 2 things that I'm very confused about.

  1. What is the advantage of using any of ES6 class or Object literals.

  2. And where should I use any of them?

Some of the examples that I'm trying out are mentioned below. Please let me know when to use particular way of implementation and when not to.

Class Example 1:

// auth.js
class Auth {
  login(req, res) {...}
  signup(req, res) {...}
}

module.exports = new Auth();

// index.js
const auth = require('auth');

Class Example 2:

// auth.js
class Auth {
  login(req, res) {...}
  signup(req, res) {...}
}

module.exports = Auth;

// index.js
const Auth = require('auth');
const auth = new Auth();

Object Literal Example:

// auth.js
module.exports = {
  login: (req, res) => {...},
  signup: (req, res) => {...}
};

// index.js
const auth = require('auth');

What I think from reading about them is that:

Class Example 1:

  • You can not create more than 1 object. Because a module is only executed once. So, on every import you will get the same object. Something similar to singleton. (Correct me here if I misunderstood it)

  • You will not be able to access the static methods of the class because you are only exporting the object of the class.

Class Example 2:

  • If you have a class that contains nothing but helper methods and the object does not have any state, It makes no sense creating object of this class all the time. So, in case of helper classes, this should not be used.

Object Literal Example:

  • You can not do inheritance.

  • Same object will be passed around on every require. (Correct me if I'm wrong here as well)

Please help me understand these concepts, what I'm missing out, what I've misunderstood and what should be used when and where. I'll be very grateful for your help.

Feel free to edit the question, if you think I made a mistake somewhere.

like image 226
Dangling Cruze Avatar asked Jun 18 '16 09:06

Dangling Cruze


1 Answers

Class Example 1: You can not create more than 1 object. Because a module is only executed once. So, on every import you will get the same object. Something similar to singleton.

Correct. This is an antipattern therefore. Do not use it. class syntax is no replacement for object literals.

You will not be able to access the static methods of the class because you are only exporting the object of the class.

Theoretically you can do auth.constructor.… but that's no good.

Class Example 2: If you have a class that contains nothing but helper methods and the object does not have any state, It makes no sense creating object of this class all the time. So, in case of helper classes, this should not be used.

Correct. Use a simple object literal instead, or even better: multiple named exports instead of "utility objects".

Object Literal Example: You can not do inheritance.

You still can use Object.create to do inheritance, or parasitic inheritance, or really anything.

Same object will be passed around on every require.

Correct, but that's not a disadvantage. If your object contains state, you should've used a class instead.

like image 150
Bergi Avatar answered Oct 14 '22 14:10

Bergi