Node.js module.exports与导出

by lazlojuly

通过lazlojuly

Node.js module.exports与导出 (Node.js module.exports vs. exports)

它们是什么,如何使用它们以及如何不使用它们 (What are they, how to use them and how not to use them)

(Note that this article was written after the Node.js 6.1.0 release)

(请注意,本文是在Node.js 6.1.0版本之后编写的)

TL; DR (TL;DR)
  • Think of module.exports as the variable that gets returned from require(). It is an empty object by default, and it is fine to change to anything.

    将module.exports视为从require()返回的变量。 默认情况下,它是一个空对象,可以更改为任何对象。
  • Exports? Well, “exports” itself is never returned! It is just a reference to module.exports; a convenience variable to help module authors write less code. Working with its properties is safe and recommended.

    出口? 好吧,“出口”本身永远不会退货! 它只是对module.exports的引用; 一个方便使用的变量,可以帮助模块作者编写更少的代码。 建议使用其属性是安全的。

exports.method = function() {…}
vs.
module.exports.method = function() {…}

一个简单的模块示例 (A simple module example)

First, we need an example codebase. Let’s start with a simple calculator:

首先,我们需要一个示例代码库。 让我们从一个简单的计算器开始:

Usage:

用法:

模块包装 (The module wrapper)

Node.js internally wraps all require()-ed modules in a function wrapper:

Node.js在内部将所有require()-ed模块包装在函数包装器中:

模块对象 (The module object)

Variable “module” is an object representing the current module. It is local to each module and it is also private (only accessible from module code):

变量“ 模块 ”是代表当前模块的对象。 它对于每个模块 都是 本地的,并且也是私有的(只能从模块代码访问):

Module.exports (Module.exports)

  • It is the object reference that gets returned from the require() calls.

    对象引用是从require()调用返回的。

  • It is automatically created by Node.js.

    它由Node.js自动创建。
  • It is just a reference to a plain JavaScript object.

    它只是对普通JavaScript对象的引用。
  • It is also empty by default (our code attaches an “add()” method to it)

    默认情况下也为空(我们的代码在其上附加了一个“ add()”方法)

There are two ways we can use module.exports:

我们可以使用两种方法来使用module.exports:

  1. Attaching public methods to it (like we did in the calculator example).

    附加公共方法 (就像我们在计算器示例中所做的那样)。

  2. Replacing it with our custom object or function.

    用我们的自定义对象或函数替换

Why replace it? When replacing, we can return any arbitrary instance of some other class. Here is an example written in ES2015:

为什么要更换它? 替换时,我们可以返回其他任何类的任意实例。 这是用ES2015编写的示例:

Above, “calculator-base” exports a class.Let’s extend “Calculator” class and export an instance this time:

上面的“基于计算器的”导出一个类。让我们扩展“计算器”的类并导出一个实例:

Usage:

用法:

导出别名 (Exports alias)

  • “exports” is just a convenience variable so module authors can write less code

    “ exports”只是一个方便变量,因此模块作者可以编写更少的代码

  • Working with its properties is safe and recommended.

    建议使用其属性是安全的。

    (eg.: exports.add = function…)

    (例如:exports.add =函数…)

  • Exports is NOT returned by require() (module.exports is!)

    导出不是由require()返回的(module.exports是!)

Here are some good and some bad examples:

这是一些好事和坏事的例子:

Note: It is common practice to replace module.exports with custom functions or objects. If we do that but still would like to keep using the “exports” shorthand; then “exports” must be re-pointed to our new custom object (also done in code above at line 12):

注意:通常的做法是用自定义函数或对象替换module.exports。 如果我们这样做,但仍然希望继续使用“出口”的简写; 然后必须将“导出”重新指向我们的新自定义对象(也在上面的第12行的代码中完成):

exports = module.exports = {}
exports.method = function() {...}

结论 (Conclusion)

A variable named exports that is not being entirely exported is confusing, especially for newcomers to Node.js. Even the official documentation has a slightly strange take on it too:

不能完全导出的名为exports的变量令人困惑,尤其是对于Node.js的新手而言。 甚至官方文档也对此有些奇怪:

As a guideline, if the relationship between exports and module.exports seems like magic to you, ignore exports and only use module.exports.
指导原则是,如果export和module.exports之间的关系对您来说像魔术一样,请忽略export,而仅使用module.exports。

My take is that code is not magic. Developers should always seek deeper understanding of the platforms and languages they use. By doing so; programmers gain valuable confidence and knowledge which in turn positively impacts code quality, system architecture and productivity.

我认为代码不是魔术。 开发人员应始终寻求对所使用平台和语言的更深入了解。 通过这样做; 程序员获得了宝贵的信心和知识,进而对代码质量,系统架构和生产率产生了积极影响。

Thank you for reading my post. Feedback and thoughts are always welcome in the comments section.

感谢您阅读我的帖子。 意见部分始终欢迎您提供反馈和想法。

lazlojuly

Lazlojuly

资料来源: (Sources:)

Checkout my new blog series on unit unit testing:

查看我有关单元单元测试的新博客系列:

How to get started with Unit Testing? Part #1I guess many of us can relate to a situation depicted above.A place where, unit testing is considered as a chore.medium.com

如何开始进行单元测试? 第一部分#1 我想我们许多人可能与上述情况有关。 一个将单元测试视为繁琐工作的地方。 medium.com

翻译自: https://www.freecodecamp.org/news/node-js-module-exports-vs-exports-ec7e254d63ac/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值