zoukankan      html  css  js  c++  java
  • module.export和export

    • 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] }"

  • 相关阅读:
    js替换问题replace和replaceAll
    CSS截取标题...
    MVC FormCollection collection
    IE和FF判断浏览器是否安装了flash插件
    Uploadify上传插件不兼容FF、Chrome等Netscape浏览器
    jquery错误消息: “Date”未定义,“String”未定义
    正则表达式取出指定字符窜之间的字符窜
    WPF 开发前学习(二)
    WPF 中的树应用
    WCF 数据传输遇到的问题
  • 原文地址:https://www.cnblogs.com/yuyuj/p/4559094.html
Copyright © 2011-2022 走看看