在Express + NodeJS应用程序的控制器中使用ES6类或对象文字 [英] Using ES6 classes OR Object Literals in controllers for an Express + NodeJS app

查看:158
本文介绍了在Express + NodeJS应用程序的控制器中使用ES6类或对象文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两件事我很困惑.

  1. 使用任何ES6类或对象文字有什么好处.

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

我应该在哪里使用它们?

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.

类示例1:

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

module.exports = new Auth();

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

类示例2:

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

module.exports = Auth;

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

对象文字示例:

// 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:

课程示例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.

课程示例2:

  • 如果您的类只包含辅助方法,并且该对象不具有任何状态,则始终创建该类的对象是没有意义的.因此,在使用辅助类的情况下,不应使用它.

对象文字示例:

  • 您不能继承.

  • 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.

如果您认为我在某个地方犯了错误,可以随时编辑问题.

推荐答案

类示例1:您不能创建多个对象.因为一个模块只执行一次.因此,在每次导入时,您将获得相同的对象.类似于单例.

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.

正确.因此,这是一种反模式.不要使用它. class语法不能替代对象文字.

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.

从理论上讲,您可以执行auth.constructor.…,但这不好.

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

类示例2:如果您有一个仅包含辅助方法的类,并且该对象没有任何状态,则始终创建该类的对象是没有意义的.因此,在使用辅助类的情况下,不应使用它.

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.

您仍然可以使用Object.create进行继承,寄生继承或其他任何事情.

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

相同的对象将在每次需要时传递.

Same object will be passed around on every require.

正确,但这不是缺点.如果您的对象包含状态,则应改用class.

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

这篇关于在Express + NodeJS应用程序的控制器中使用ES6类或对象文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆