对于大多数node初学者而言, module.exports应该都是理解的, 但多出来一个exports获取就有些疑问了

疑问一: 既然有module.exports了为什么还要有exports?

疑问二: 两者有什么区别?

首先, 官网是这么回答的

  The exports variable is available within a module's file-level scope, and is assigned the value of module.exports before the module is evaluated.

  It allows a shortcut, so that module.exports.f = ... can be written more succinctly as exports.f = ....

也就是说, exports相当于一个快捷方式,exports.f = ....  肯定是比 module.exports.f = ... 写起来方便一些。下面附上一段express源码中的使用你就明白了。

exports = module.exports = createApplication;

exports.mime = connect.mime;
exports.application = proto;
exports.request = req;
exports.response = res; function createApplication() {
var app = connect();
merge(app, proto);
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.init();
return app;
}

其实exports是将指针执行module.exports对象, 如果你像exports = function(){}这样相当于改变了exports原来的指向, 也就无法被导出, 为什么?先看官网给的类似实现:

function require(/* ... */) {
const module = { exports: {} };
((module, exports) => {
// 你的模块代码在这。在这个例子中,定义了一个函数。
function someFunc() {}
exports = someFunc;
// 此时,exports 不再是一个 module.exports 的快捷方式,
// 且这个模块依然导出一个空的默认对象。
module.exports = someFunc;
// 此时,该模块导出 someFunc,而不是默认对象。
})(module, module.exports);
return module.exports;
} 

根据上面的代码可以看出exports是模块内部一个形参对象, 如果给exports对象添加属性是可以导出的, 因为指针并未改变, 但如果赋值一个对象就不行了, 因为指针已经改变了,最后导出的是module.exports

module.export与export的区别?的更多相关文章

  1. node.js中module.export与export的区别。

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

  2. module.exports,exports,export和export default,import与require区别与联系【原创】

    还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...

  3. Node.js模块导出module.exports 和 exports,Es6模块导出export 和export default的区别

    1.module.exports  module变量代表当前模块.这个变量是一个对象,module对象会创建一个叫exports的属性,这个属性的默认值是一个空的对象: module.exports ...

  4. exports与module.exports的区别,export与export.defult区别

    在JS模块化编程中,之前使用的是require.js或者sea.js.随着前端工程化工具webpack的推出,使得前端js可以使用CommonJS模块标准或者使用ES6 moduel特性. 在Comm ...

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

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

  6. module.exports,exports,export和export default,import与require区别与联系

    还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...

  7. export、export default、module.export区别

    在es6里面定义模块,导出模块时可以使用export.export default 这2者区别: 在同一个文件里面可以有多个export, 一个文件里面只能有1个export default //a. ...

  8. exports与module.exports的区别,以及export与export.defult的区别

    在 JS 模块化编程的模块引入上, 主要有两种方式: CommonJS 模块标准 ES6 moduel 特性 1. CommonJS 模块引入:require() 模块导出:exports 或者 mo ...

  9. export,export default,module.exports,import,require之间的区别和关联

    module.exports Node 应用由模块组成,采用 CommonJS 模块规范.根据这个规范,每个文件就是一个模块,有自己的作用域.在这些文件里面定义的变量.函数.类,都是私有的,对外不可见 ...

  10. module.exports与exports,export与export default的区别

    首先我们要明白一个前提,CommonJS模块规范和ES6模块规范完全是两种不同的概念. CommonJS模块规范 Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个 ...

随机推荐

  1. 在Ubuntu上安装boost库[转]

    在编译kenlm的时候需要安装boost,去官网下载boost安装包,然后按照以下步骤安装. boost官网 -----------------以下内容,网上转载------------------- ...

  2. oracle建存储过程

    进入plsql命令行 [10:42:10 liuyi@localhost]/home/liuyi>sqlplus demo/demo@180.200.3.129/meboss 连接串格式:用户名 ...

  3. Warning: Attempt to present A on B whose view is not in the window hierarchy!

    昨天写豆瓣发广播Demo的时候,为了写Demo的简单,就使用了Storyboard,结果执行视图跳转时遇到了这个问题: Warning: Attempt to present <UINaviga ...

  4. Devexpress VCL Build v2014 vol 14.1.4 发布

    虽然这次没加什么新东西,但是及时更新支持xe7,还算可以. What's New in 14.1.4 (VCL Product Line)   New Major Features in 14.1 W ...

  5. 551. Student Attendance Record I

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  6. 2018.08.30 NOIP模拟 graph(dfs序/树剖+线段树)

    [描述] 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点 到达. 2. 接下来的 N ...

  7. 2018.07.17 牛奶模式Milk Patterns(二分+hash)

    传送门 一道简单的字符串.这里收集了几种经典做法: SAM,不想写. 后缀数组+二分,不想写 后缀数组+单调队列,不想写 hash+二分,for循哈希,天下无敌!于是妥妥的hash 代码如下: #in ...

  8. 软件工程—WC功能实现 (JAVA)

    软件工程-WC功能实现(JAVA) Github项目地址:https://github.com/Ousyoung/wc 项目要求 ​ wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和 ...

  9. [label][JavaScript]七个JavaScript技巧

    重点:http://www.javascriptkit.com/ create an object: var car = new Object(); car.colour = 'red'; car.w ...

  10. WinRT 中后台任务类的声明

    要实现后台任务,需要实现IBackgroundTask接口 public sealed class SimpleTask : IBackgroundTask { public void Run(IBa ...