Node: module.exports、exports的区别



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

    1. exportsmodule.exports的一个引用,exports指向的是module.exports
    2. mdoule.exports初始化为一个空对象
    3. require()返回的时**module.exports而不是exports**

    知道以上三点后我们可以知道:

    当给exports对象添加属性时,我们同时也操作了module.exports,是可行的;

    exports.a = function(){};
    

    当给exports对象赋值时,断开了exportsmodule.exports的连接,moudle.exports仍然是空对象,这时就无效了

    exports = {
      a: a,
      b: b
    }
    

    因此建议,无论何时都使用module.exports的形式导出模块


 

Copyright © 2018 bbs.dian.org.cn All rights reserved.

与 Dian 的连接断开,我们正在尝试重连,请耐心等待