• module.exports 和 exports 是引用到的同一个对象,类似下面代码所示(为了举例,不是完全的正确):

var module.exports = {};
        var exports = module.exports;
        在模块内,没对module.exports和exports做任何更改前,他们都是指向同一个空对象。

  • 在其他模块内使用require引用的是module.exports 对象,不一定是exports指向的引用对象,这点在开发中很容易因为忽略而导致bug产生。如下例子:

exports = function(){};
        //在其他模块内
        require(...);//得到{}
        此时在其他模块内使用require引用上面模块导出的对象时,发现为空对象,为什么?因为使用exports=function(){}时,是将exports的引用指向了这个函数,而module.exports的引用还是空对象,并且其       他模块使用require引用的是module.exports,所以此处会得到空对象,而不是函数。当然此处写成module.exports = function(){}肯定是没问题的。

  • 有人说为了防止上面的bug产生,一般都执行下 exports = module.exports,,但是直接覆盖module.exports不是一个好习惯,所以对于 module.exports = {a:123, b:456} 这样的情况,分开来写:

exports.a = 123;
             exports.b = 456;

  • 下面贴下几种情况,其他模块得到的不同结果:

exports = function fn(){}; // outputs "@routes {}"
        exports.fn = function fn(){}; // outputs "@routes { fn: [Function: fn] }"
        module.exports = function fn(){}; // outputs "@routes function fn(){}"
        module.exports.fn = function fn(){}; // outputs "@routes { fn: [Function: fn] }"

module.export和export的更多相关文章

  1. module.exports与exports,export和export default

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

  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. export与export default exports与module.exports的用法

    转载:http://blog.csdn.net/zhou_xiao_cheng/article/details/52759632 本文原创地址链接:http://blog.csdn.net/zhou_ ...

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

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

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

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

  8. export、export default、module.export区别

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

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

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

  10. exports、module.exports 和 export、export default

    先了解他们的使用范围. require: node 和 es6 都支持的引入export / import : 只有es6 支持的导出引入module.exports / exports: 只有 no ...

随机推荐

  1. 关于Linux:chmod和chown知识

    将vendor下的root权限递归更改为user命令:sudo chown -R user:user fujitsu 指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [- ...

  2. sql搜索like通配符的用法详解

    http://www.lmwlove.com/ac/ID878 有很多朋友写了几年的like搜索,可能对like后面通配符的用法都不了解,甚至于%的作用是什么都不清楚.在这篇文章中,我们就一起来学习一 ...

  3. Getting Started

    https://developers.google.com/v8/get_started Getting Started This document introduces some key V8 co ...

  4. type

    MollyPages.org"You were wrong case.To live here is to live." Home Pages / Database / Forms ...

  5. SDF文件的用途

    标准延迟格式(英语:Standard Delay Format, SDF)是电气电子工程师学会关于集成电路设计中时序描述的标准表达格式.在整个设计流程中,标准延迟格式有着重要的应用,例如静态时序分析和 ...

  6. ANdroid5.0不能隐式启动service,必须显示,解决办法,加服务端包名

    Intent intent = new Intent(); intent.setAction("com.viaembedded.veonvif.RemoteService");// ...

  7. Device eth0 does not seem to be present, delaying initialization. 问题

    今天在复制vmware的时候 出现网卡无法启动 报错显示 Device eth0 does not seem to be present, delaying initialization. 这个错误原 ...

  8. sql group by datetime on day

    SELECT        DATEADD(dd, DATEDIFF(dd, 0, postDate), 0)FROM          tableWHERE        (postDate > ...

  9. C# 模拟键盘操作--SendKey(),SendKeys()

    模拟键盘输入就是使用以下2个语法实现的.SendKeys.Send(string keys);  //模拟汉字(文本)输入SendKeys.SendWait(string keys); //模拟按键输 ...

  10. mongodb的linux环境搭建

    一.启动 [mongodb@node1 ~]$ mongod -f /data/config/shard1.confmongod: /usr/lib64/libcrypto.so.10: no ver ...