在JS模块化编程中,之前使用的是require.js或者sea.js。随着前端工程化工具webpack的推出,使得前端js可以使用CommonJS模块标准或者使用ES6 moduel特性。

在CommonJs模块标准中我们载入模块使用的是require(),输出模块用的是exports或者module.exports

在ES6中载入模块我们用的是import ,输出模块用的是export

exports与module.exports的区别

//载入模块
var m = require('./moduleA.js')
m.callName()
//输出模块
exports.callName = function(){
console.log('jesse')
}
//也可以这样输出
module.exports.callName = function(){
console.log('jesse')
}

module.exports才是module模块的真正接口,而exports可以理解为它的一个副本

虽然修改exports对象的时候也会修改module.exports对象,但最终返回给调用的是module.exports对象

当module.exports对象通过赋值方式进行设定后,就已经和exports对象没关系了

so,我的理解是exports输出的只是要输出的对象的某个属性,module.exports才真正输出的是要输出的对象。

一个模块文件中可以有多个exports输出,但只能有一个module.exports输出

这种方式普遍用于node中模块的编写

export与export.defult区别

es6moduel特性在node环境中并不能完全支持,解决方法是用babel编译

//载入模块
import {callName} from './moduleA.js'
callName()
//输出模块
export function callName (){
console.log('jesse')
}

1.export与export default均可用于导出常量、函数、文件、模块等

2.你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对其进行使用

3.在一个文件或模块中,export、import可以有多个,export default仅有一个

4.通过export方式导出,在导入时要加{ },export default则不需要

exports与module.exports的区别,export与export.defult区别的更多相关文章

  1. exports与module.exports的区别,以及export与export.defult的区别

    在 JS 模块化编程的模块引入上, 主要有两种方式: CommonJS 模块标准 ES6 moduel 特性 1. CommonJS 模块引入:require() 模块导出:exports 或者 mo ...

  2. exports与module.exports,export与export default 之间的关系和区别

    首先我们要明白一个前提,CommonJS模块规范和ES6模块规范完全是两种不同的概念. CommonJS模块规范 Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个 ...

  3. exports、module.exports 和 export、export default

    先了解他们的使用范围. require: node 和 es6 都支持的引入export / import : 只有es6 支持的导出引入module.exports / exports: 只有 no ...

  4. NodeJS的exports、module.exports与ES6的export、export default深入详解

    前言 决定开始重新规范的学习一下node编程.但是引入模块我看到用 require的方式,再联想到咱们的ES6各种export .export default. 阿西吧,头都大了.... 头大完了,那 ...

  5. nodejs模块中exports和module.exports的区别

    通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ ,这些核心模块被编译成二进制文件,可以require('模块名')去获取:核心 ...

  6. node exports和module.exports区别

    我们只需知道三点即可知道 exports 和 module.exports 的区别了: exports 是指向的 module.exports 的引用 module.exports 初始值为一个空对象 ...

  7. exports和module.exports的区别

    总结:exports是module.exports的指向. 1. module应该是require方法中,上下文中的对象 2. exports对象应该是上下文中引用module.exports的新对象 ...

  8. nodejs中exports与module.exports的区别详细介绍

    如果模块是一个特定的类型就用Module.exports.如果模块是一个典型的"实例化对象"就用exports. exports.name = function() { conso ...

  9. 【nodejs】exports 和 module.exports 的区别

    require 用来加载代码,而 exports 和 module.exports 则用来导出代码.但很多新手可能会迷惑于 exports 和 module.exports 的区别,为了更好的理解 e ...

随机推荐

  1. 《用Python解决数据结构与算法问题》在线阅读

    源于经典 数据结构作为计算机从业人员的必备基础,Java, c 之类的语言有很多这方面的书籍,Python 相对较少, 其中比较著名的一本 problem-solving-with-algorithm ...

  2. .NET Core + Abp踩坑和填坑记录(1)

    1. Net Core 的DI和Abp的DI并存 Startup中 ConfigureServices返回值改为IServiceProvider 在ConfigureServices最后调用retur ...

  3. 静态和实例方法区别-java

    静态方法和实例方法的区别主要体现在两个方面:   在外部调用静态方法时,可以使用"类名.方法名"的方式,也可以使用"对象名.方法名"的方式.而实例方法只有后面这 ...

  4. 不一样的ssm

    这里的ssm不是指的spring+springmvc+mybatis,而是指的spring+springmvc+mongodb,下面我将搭建一个简单的“ssm”框架. 1.新建一个maven项目,骨架 ...

  5. 如何一步步在生产环境上部署django和vue

    本文由云+社区发表 本文主要讲述了如何一步步在生产环境上部署django和vue,操作系统默认为centos 说明:后文中出现的以下字符串均表示具体的路径或者名称,含义如下: DJANGO_DIR-- ...

  6. Flask入门第一天

    一.flask介绍 flask诞生于2010年,是Armin ronacher用python语言基于Werkzeug工具箱编写的轻量级web开发框架.flask本身相当于一个内核,其他所有的功能都需要 ...

  7. vue_drf之多级过滤、排序、分页

    一.前端代码 1,父组件free_course.vue <template> <div id="free_course"> <el-container ...

  8. [转]angular2: including thirdparty js scripts in component

    本文转自:https://stackoverflow.com/questions/35570746/angular2-including-thirdparty-js-scripts-in-compon ...

  9. [转]【Angular4】基础(六):HTTP模块

    本文转自:https://blog.csdn.net/u013451157/article/details/79519719 版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...

  10. 【转载】 Sqlserver中DateAdd()函数

    在Sqlserver数据库中,DATEADD() 函数在日期中添加或减去指定的时间间隔.例如计算当前时间往后一天的时刻以及往前1天的时刻时间即可使用DateAdd()函数来操作,DateAdd()函数 ...