彻底搞懂 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不行;

原文地址:https://www.cnblogs.com/idspring/p/12271439.html

时间: 2024-08-05 20:07:36

彻底搞懂 module.exports/exports/import/export/export default的相关文章

ES6学习笔记<五> Module的操作——import、export、as

import export 这两个家伙对应的就是es6自己的 module功能. 我们之前写的Javascript一直都没有模块化的体系,无法将一个庞大的js工程拆分成一个个功能相对独立但相互依赖的小工程,再用一种简单的方法把这些小工程连接在一起. 这有可能导致两个问题: 一方面js代码变得很臃肿,难以维护 另一方面我们常常得很注意每个script标签在html中的位置,因为它们通常有依赖关系,顺序错了可能就会出bug 在es6之前为解决上面提到的问题,我们得利用第三方提供的一些方案,主要有两种

module.exports、exports、export、import、export default区别

CommonJs规范:module.exports.exports module.exports和exports 是commonJs的语法,大家熟知的node就是基于CommonJs语法设计的,node将每个文件视为一个拥有独立作用域链的模块, 每个木块的类,变量,函数等都是私有的,对其他文件不可见.但是,如果别的模块想要使用另一个模块的函数应该如何处理呢,这时我们就会用到module.exports.exports了. node将每个独立的文件视为一个mudule,而exports是为了将本模

探索 模块打包 exports和require 与 export和import 的用法和区别

菜单快捷导航: CommonJS 之 exports和require用法 ES6 Module 之 export 和 import 用法 CommonJS和ES6 Module的区别 循环依赖 和 解决办法 模块打包原理简析 1.CommonJS 之 exports和require用法 CommoneJS规定每个文件是一个模块.将一个JavaScript文件直接通过script标签引入页面中,和封装成CommonJS模块最大的不同在于:前者的顶层作用域是全局作用域,在进行变量及函数声明时会污染全

转:彻底搞清楚javascript中的require、import和export

为什么有模块概念 理想情况下,开发者只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块. 但是,Javascript不是一种模块化编程语言,在es6以前,它是不支持”类”(class),所以也就没有”模块”(module)了. require时代 Javascript社区做了很多努力,在现有的运行环境中,实现”模块”的效果. 原始写法 模块就是实现特定功能的一组方法.只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. 1 2 3 4 5 6 function m1()

nodeJS exports – exports vs module.exports

require, exports, module.exports 区别: require 用来加载代码(模块),而 exports 和 module.exports 则用来导出代码. exports 是指向的 module.exports 的引用 module.exports 初始值为一个空对象 {},所以 exports 初始值也是 {} require() 返回的是 module.exports 而不是 exports nodejs中exports与module.exports的区别: Mo

require、module、exports dojo中的三个特殊模块标识

查看dojo源码过程中,发现这三个模块名并不以dojo.dijit.dojox开头,在dojo加载器中属于特殊模块名. require 这是一个上下文智能的加载器. 我们通过dojoConfig配置了一个package:myApp,现在对myApp目录下的文件可以使用如下方式请求: // this is "myApp/topLevelHandlers" define(["dojo"], function(dojo){ dojo.connect(dojo.byId(&

js的import 与export详解

ES6 1.export default 其他模块加载该模块时,import命令可以为该匿名函数指定任意名字. 如: import Vue from 'vue' vue里面的第三方模块都是用了这个 使用import 不带{ }如上,一定要用export default 导出,不能用export导出: 显然,一个模块只能有一个默认输出,因此export default命令只能使用一次. export defalut 只能用import boy from '模块路径',不能带{} 所以,import

汇编里的IMPORT和EXPORT

IMPORT ,定义表示这是一个外部变量的标号,不是在本程序定义的EXPORT ,表示本程序里面用到的变量提供给其他模块调用的.以上两个在汇编和C语言混合编程的时候用到刚看到一篇不错的BLOG,解说C和汇编混编的,虽然貌似是翻译ADS文档的,不过写的挺不错,通俗容易懂,可以看看其实汇编调用C貌似很简单B Main 就完了,直接跳过去,那传递参数怎么办?根据<嵌入式系统 Boot Loader 技术内幕>里面说的用弹簧床什么的来结局,不过暂时理解不了.用ADS的一个项目中同时放汇编的S文件和C语

react 中使用import()实现按需加载报错 解决方法 --‘import’ and ‘export’ may only appear at the top level

因为项目需要搞一下按需加载,使用import实现代码打包分片按需加载,但是在实际使用中报语法错误.报错信息如下 SyntaxError: 'import' and 'export' may only appear at the top level 啊咧?报错了. 查找发现很多人碰到过,解决方法不同,但是我这个报错适用下边这个方法. npm install --save-dev babel-plugin-syntax-dynamic-import 然后调整babel-loader配置如下: use