zoukankan      html  css  js  c++  java
  • module.exports 和 exports的区别

     

    引用传递的理解 就理解了这两者的区别
    
       var arr = [10,20,30];
        var newarr = arr;
    
        console.log(arr);//[10,20,30]
        console.log(newarr);//[10,20,30];
    
        
        newarr[0] = 40;
        console.log(arr);//[40,20,30];
        console.log(newarr);//[40,20,30];
    

     

      

     

     

    例一. 采用module.exports

    module.exports = {
        hello: "hello",
        world: "world"
    };
    

    例二. 采用exports

    exports.hello = "hello";
    exports.world= "world";
    

      

       区别   

    exports = {
        hello: "hello",
        world: "world"
    };//这样代码不报错, 但是没有用, 接口并未暴露出来. 原因请看下面

      

    var load = function (exports, module) {
        // .js的文件内容
        ...
        // load函数返回:
        return module.exports;
    };
    
    var exported = load(module.exports, module);
    
    

      

    系统自动给nodejs 文件增加2个变量 exports 和 module, module 又有一个属性 exports, 这个exports 属性指向一个空对象 {}; 同时 exports这个变量也指向了这个空对象{};

    于是就有了 exports => {} <=module.exports.

    就是说自己定义一个exports没有用的,必须给modul。exports设置key:value值(exports.text= "hello")才可以,因为最后是导出 module.exports。

    (外加:node.js是遵循common.js 实用const ** = require("**") 和 module.exports暴露模块,而浏览器v8引擎是解析js脚本文件的,并不能运行node.js的模块。

    推荐用module.exports导出。

  • 相关阅读:
    SAP MM MIGO过账报错
    SAP MM MB5L事务代码'仅总计'选项初探
    SAP MM 巴西采购订单中的NCM Code
    SAP MM Storage Location Missing in MD04 Result?
    SAP MM 预留单据的历史修改记录?
    2018-8-10-上传代码-CodePlex
    2019-9-2-win10-uwp-九幽图床
    2018-2-13-win10-UWP-应用设置
    2018-2-13-win10-UWP-你写我读
    2018-2-13-win10-UWP-九幽登录
  • 原文地址:https://www.cnblogs.com/moonzwt/p/9794727.html
Copyright © 2011-2022 走看看