Using ES6 classes OR Object Literals in controller

2020-06-09 02:33发布

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.

2条回答
戒情不戒烟
2楼-- · 2020-06-09 03:16

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.

查看更多
地球回转人心会变
3楼-- · 2020-06-09 03:33

If your class has got a constructor, you can build several objects from this class threw :

var Panier= require('./panier');    
var panier1 = new Panier(13, 12, 25);
var panier2 = new Panier(1, 32, 569);

Of course your Panier would be defined in the file Panier.js located in the same directory :

module.exports = class Panier
{
    constructor(code, qte, prix)
    {
        this.codeArticle = code;
        this.qteArticle = qte;
        this.prixArticle = prix;
    }
    getCode()
    {
        return this.codeArticle;
    }
    getQte()
    {
        return this.qteArticle;
    }
    getPrix()
    {
        return this.prixArticle;
    }
}
查看更多
登录 后发表回答