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

前端 未结 2 964
天涯浪人
天涯浪人 2021-02-07 13:49

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

2条回答
  •  被撕碎了的回忆
    2021-02-07 14:14

    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;
        }
    }
    

提交回复
热议问题