• 彻底搞懂 module.exports/exports/import/export/export default


    module.exports/exports

    module.exports 是模块系统创建的(全局内置对象);当你创建完成一个模块时,需要将此模块暴露出去,以便使用;module.exports 便提供了暴露出去的接口方法;

    • 例如暴露出去一个对象(暴露一个全局变量或方法):
    /**创建模块 module.exports.js */
    let object = {
      name: 'zhangsan',
      age: 24,
      hasSay: () => {
        console.info('This is zhangsan')
      }
    }
    
    module.exports = object;
    
    /**引用模块 server.js */
    const Obj = require('./module.exports.js')
    console.log('---->', Obj)

    或者:

    /**创建模块 module.exports.js */
    function fn1 () {
      console.info('fn1');
    }
    function fn2 () {
      console.info('fn2');
    }
    
    exports.fn1 = fn1;
    exports.fn2 = fn2;
    
    
    /**引用模块 server.js */
    const fn = require('./module.exports.js')
    console.log('---->', fn.fn1, fn.fn2);
    • 暴露出去构造函数(类):
    /**创建模块 module.exports.js */
    class Person {
      constructor (name) {
        this.name = name
        console.info('name:', name);
      }
    }
    
    module.exports = Person;
    
    
    /**引用模块 server.js */
    const Person = require('./module.exports.js')
    console.log('---->', new Person('lisi'));
    • 说到底那 module.exports 和 exports 有啥区别呢?

      1. 语法区别:

    exports.[function name] = [function name]
    
    moudle.exports= [function name]

      2. 本质区别:

      exports 暴露出的是一个模块中的某个函数;

      module.exports 暴露出模块对象本身(其实就是一个类);

      3. 使用区别:

      exports 暴露出的函数可以直接调用;

      module.exports 暴露出的类需要实例出对象;

    注意:当 module.exports 和 exports 同时存在于一个模块中时,以 module.exports 暴露出去的方法为主;

    •  exports

    /**创建模块 module.exports.js */
    function fn () {
      console.info('fn');
    }
    
    exports.fn = fn;
    console.log('module.exports->', module.exports); // Object { fn: }
    console.log('exports->', exports); // Object { fn: }
    console.log('查看两者是否相等:', module.exports === exports); // true

    module.exports 和 exports 是一个对象并且都有 fn 方法;module.exports === exports 结果为 true,说明两者指向同一个对象;

    • module.exports

    /**创建模块 module.exports.js */
    function fn () {
      console.info('fn');
    }
    
    module.exports = fn;
    console.log('module.exports->', module.exports); // fn () {...}
    console.log('exports->', exports); // {}
    console.log('查看两者是否相等:', module.exports === exports); // false

    此时module.exports 和 exports 两者的指向不同;module.exports 地址指向 fn 方法;exports 地址指向还是原来对象;

    • export / import / export default

    CommonJs 规范(module.exports / exports ; require);

    ES6(export / import);

    require : node 和 ES6 都支持的导入;

    export / import: ES6支持的导入和导出;

    module.exports / exports:node支持的导出;

    • Node

    我们需要知道 Nodejs 里面的模块系统都是遵循 CommonJs 规范的;

    CommonJs 定义的模块分为:模块标识(module);模块定义(exports);模块引入(require)

    node在执行一个文件时,会在文件中生成一个exports 和 module 对象,module 对象又有一个exports 属性。

    exports 和 module.exports  的关系:两者都是指向同一个对象;

    exports = module.exports = {}
    • ES6中模块的导出 / 导入

    导出 export / export default 两者的区别:

    1. export与export default均可用于导出常量、函数、文件、模块等;
    2. 在一个文件或模块中,export、import可以有多个,export default仅有一个;
    3. 通过export方式导出,在导入时要加{ },export default则不需要;
    4. export能直接导出变量表达式,export default不行;
  • 相关阅读:
    linux嵌入式系统交叉开发环境
    Codeforces Round #208 E. Dima and Kicks
    mvn 编译错误java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter. <init>(Z)V
    黑马程序员_<<TCP>>
    微信/易信公共平台开发(四):公众号调试器 (仿真微信平台,提供PHP源码)
    用pdb调试OpenStack Havana
    MySql Odbc等驱动下载地址分享下
    导入exce表格中的数据l到数据库
    关闭数据备份信息写入数据库日志
    SQL Server之RAID简介
  • 原文地址:https://www.cnblogs.com/idspring/p/12271439.html
Copyright © 2020-2023  润新知