module.exports和exports

写node的时候,特别是自定义模块的时候,都是一顿乱敲,然后module.exports={}完事。

但有时候去看别人写的代码的时候会发现还可以exports,比如导出一个函数exports.fn = function(){}这样总少写了module,感觉更简洁。

为了让自己写代码更快乐一点,我决定搞清楚它们的区别。

查看官方文档,谷歌一波。

最后发现,exports只是module.exports的全局引用,原文是这样说的:

"exports is assigned the value of module.exports before the module is evaluated."

意思是:在模块被导出之前exports被赋值为module.exports。

用中文的话来说就是一个全局引用,在模块内部你都可以使用exports导出一些东西。

值得注意的是,你不能给exports赋值,这很重要,很重要,很重要。

比如,你想导出一个类。

class girl {
constructor(age) {
this.age = age
}
love(u) {
return "你是个好人!"
}
}
// 把她导出去
exports = girl // 出错! 不能重写exports!
// 正确做法
module.exports = girl

总结:

  1. exports只是module.exports的引用
  2. 如果你想要使用exports导出模块,千万不能给它赋值!
  3. 稳一点的做法是使用module.exports,虽然exports用起来是挺爽的

浅析module.exports和exports区别和使用的更多相关文章

  1. node (02 CommonJs 和 Nodejs 中自定义模块)顺便讲讲module.exports和exports的区别 dependencies 与 devDependencies 之间的区别

    CommonJS 规范的提出,主要是为了弥补当前 JavaScript 没有标准的缺陷.它的终极目标就是:提供一个类似 Python,Ruby 和 Java 语言的标准库,而不只是停留在小脚本程序的阶 ...

  2. 【node】------module.exports&&exports之间的区别------【巷子】

    1.再讲module.exports 与exports之间的区别的时候我们先来回顾一下js里面的引用传递 001.引用传递 var arr = [10,20,30]; var newarr = arr ...

  3. module.exports 、exports、export、export default的区别

    module.exports和exports是属于 CommonJS 模块规范,export和export default是属于ES6语法. module.exports和exports导出模块,用r ...

  4. module.exports与exports的联系与区别

    首先说明他们是啥? 在CommonJS规范中,exports和module.exports这两个对象是把某一模块化文件中的属性和方法暴露给外部模块的接口(说法可能不准确),外部模块通过require引 ...

  5. module.exports和exports得区别

    对module.exports和exports的一些理解 可能是有史以来最简单通俗易懂的有关Module.exports和exports区别的文章了. exports = module.exports ...

  6. Module.exports和exports的区别

    原文链接: https://www.ycjcl.cc/2017/02/10/module-exportshe-exportsde-qu-bie/ 学习Seajs时,看到了exports.doSomet ...

  7. module中module.exports与exports的区别(转)

    转https://cnodejs.org/topic/55ccace5b25bd72150842c0a require 用来加载代码,而 exports 和 module.exports 则用来导出代 ...

  8. NodeJS2-3环境&调试----module.exports与exports的区别

    exports默认会给他设置为module.exports的快捷方式,可以把它的里面添加属性,但是我们不能修改它的指向,如果修改了它的指向那它和普通对象没有任何区别了.因为在CommonJS中,模块对 ...

  9. Node.js module.exports和exports的区别

    require 用来加载代码,而 exports 和 module.exports 则用来导出代码,从接触node.js就不会它们两陌生,上代码: foo.js exports.a = functio ...

随机推荐

  1. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  2. 自定义CheckBox

    自定义android的CheckBox按钮图形有两个步骤三种方式: 第一步: 新建Android XML文件,类型选Drawable,根结点选selector,放置在drawable文件夹内,指定各种 ...

  3. Why is the ibdata1 file continuously growing in MySQL?

    We receive this question about the ibdata1 file in MySQL very often in Percona Support. The panic st ...

  4. POJ2236:Wireless Network(并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 39772   Accepted: 164 ...

  5. codeforces 1077F1

    题目:https://codeforces.com/contest/1077/problem/F1 题意: 你有n幅画,第i幅画的好看程度为ai,再给你两个数字k,x 表示你要从中选出刚好x幅画,并且 ...

  6. ZooKeeper Watcher注意事项

    zookeeper watch的定义如下:watch事件是一次性触发器,当watch监视的数据发生变化时,通知设置了该watch的client,即watcher. 需要注意三点: 1.一次性触发器 c ...

  7. java基础学习(一)hashcode

    hashcode的作用 hashCode()方法是从Object类继承过来的,Object类中的hashCode()方法返回的是对象在内存中地址转换成的int值,如果对象没有重写hashCode()方 ...

  8. poj1379 Run Away

    传送门:http://poj.org/problem?id=1379 [题解] 题目大意:求(0,0)->(X,Y)内的一个点,使得这个点到给定的n个点的最小距离最大. 模拟退火 一开始可以先把 ...

  9. jupyter、flask、tornado、djiango安装

    安装了pip包的话直接使用: 1.安装jupyter:pip install jupyter 2.安装flask: pip install flask 3.安装tornado:pip install ...

  10. CF 200 div.1 A

    2013-10-11 16:45 Rational Resistance time limit per test 1 second memory limit per test 256 megabyte ...