module.exports 、exports、export、export default的区别
module.exports和exports是属于 CommonJS 模块规范,export和export default是属于ES6语法。
module.exports和exports导出模块,用require引入模块。
export和export default导出模块,import导入模块。
Node应用由模块组成,采用CommonJS模块规范。根据这个规范,每个文件就是一个模块,有自己的作用域。在一个文件里面定义的变量、函数、类,都是私有的,对其他文件不可见。
CommonJS规范规定,每个模块内部,module变量代表当前模块。这个变量是一个对象,它的exports属性(即module.exports)是对外的接口。加载某个模块,其实是加载该模块的module.exports属性。
1.针对CommonJs:
使用步骤:
(1)使用module.exports导出模块:
新建一个文件demo.js,通过module.exports输出变量x和函数add。
var x = 1;
var add = function (val) {
return val + x;
};
module.exports.x = x;
module.exports.add = add;
(2)使用require引入模块
require方法用于加载模块。
var demo = require('./demo.js');
console.log(demo.x); //
console.log(demo.add(1)); //
exports 与 module.exports
为了方便,Node为每个模块提供一个exports变量,指向module.exports。这等同在每个模块头部,有一行这样的命令。
var exports = module.exports;
exports其实是module.exports的引用 ,可以直接在exports对象上添加相关的方法。
2.ES6
通过export方式导出,在导入时要加{ },export default则不需要,使用export default命令,为模块指定默认输出,这样就不需要知道所要加载模块的变量名。
具体使用:
(1)export导出:
//demo1.js
export const str = 'hello world' //变量 export function fuunc(a){ //函数
return a+1
}
对应的导入方式:
//demo2.js
import { str, func } from 'demo' //也可以分开写两次,导入的时候带花括号
(2)export default
//demo1.js
export default const str = 'hello world'
对应的导入方式:
//demo2.js
import str from 'demo1' //导入的时候没有花括号
总结一些用法上的区别:
(1)module.exports和exports的用法是后面加一个等号,再接具体的导出
module.exports=...
exports=...
(2)export和export default的用法是后面直接接具体的导出,没有等号.
export ...
export default ...
module.exports 、exports、export、export default的区别的更多相关文章
- module.exports,exports,export和export default,import与require区别与联系【原创】
还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...
- Node.js模块导出module.exports 和 exports,Es6模块导出export 和export default的区别
1.module.exports module变量代表当前模块.这个变量是一个对象,module对象会创建一个叫exports的属性,这个属性的默认值是一个空的对象: module.exports ...
- module.exports,exports,export和export default,import与require区别与联系
还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...
- module.exports与exports,export与export default的区别
首先我们要明白一个前提,CommonJS模块规范和ES6模块规范完全是两种不同的概念. CommonJS模块规范 Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个 ...
- exports module.exports export export default之间的关系
exports 和module.exports是CommonJS模块规范 export export default是ES6模块的规范,两者完全是不同的概念. node应用由模块组成,采用的是Comm ...
- 你可以说出export export default || model.exports exports 的区别吗(一)
一.前言: 用模块写代码,为什么要用模块来写代码:ES6之前,在js中定义的一切,都是共享一个全局作用域的,随着web应用变得复杂,这样做会引起如:命名冲突和安全问题.于是引入了模块. 二.清楚一个概 ...
- require/exports 和 import/export 区别
零.区别 1.require/exports 是 CommonJS 的标准,适用范围如 Node.js 2.import/export 是 ES6 的标准,适用范围如 React 一.间接获取对象 ( ...
- require/exports 与 import/export 的区别?
文章作者:寸志链接:https://www.zhihu.com/question/56820346/answer/150724784来源:知乎 遵循的模块化规范不一样 模块化规范:即为 JavaScr ...
- node.js module.exports & exports & module.export all in one
node.js module.exports & exports & module.export all in one cjs const log = console.log; log ...
随机推荐
- VS2010快捷键大全----养成良好的习惯
VS2010版快捷键Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O ...
- 第106天:Ajax中同步请求和异步请求
同步请求和异步请求的区别 1.同步是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式. 用户填写所有信息后,提交给服务器,等待服务器的回应(检验数据),是一次性的.信息错误又要重新 ...
- C++的const类成员函数
转自:http://blog.csdn.net/lihao21/article/details/8634876 我们知道,在C++中,若一个变量声明为const类型,则试图修改该变量的值的操作都被视编 ...
- OATable中column栏位数据居中的实现方法及代码
// Table column中显示居中的实现 // QpriceResultTable1 为table的名称 // noPrice 为table中的列 OATableBean table = (OA ...
- windows配置Scrapy爬虫框架
一.环境 Windows10 64位 Python2.7.13 64位 下面的安装步骤最好配置代理,可能会遇到被墙的情况. 二.Python的安装 可以去参考这篇文章:http://blog.csdn ...
- jacoco生成覆盖率
要统计自动化的覆盖率,所以临时看了下jacoco,记录下过程.后续用的时候方便查看. 1.build.gradle中修改 apply plugin: 'jacoco' android { buildT ...
- MQTT再学习 -- 搭建MQTT服务器及测试
最近在搞 PM2.5 采集,需要用到 MQTT 传输协议.协议部分看了几天的,讲的七七八八.本身在 intel 上有 写好的MQTT 的源码,现在的工作其实也就是移植到单片机上或者DM368板卡上.不 ...
- IOS NSBundle的使用,注意mainBundle和Custom Bundle的区别
1.[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器 Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样: NSString *earth ...
- js字符串的裁剪
一.JavaScript字符串的处理方法 1.split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=str. ...
- Yii在window下的安装方法
首先,在http://www.yiichina.com/上下载yii 然后,配置系统环境变量,在win8下,按win+x,找到系统->高级系统设置->环境变量->path 把php的 ...