exports与module.exports的区别
nodejs有自己的模块系统,分为文件模块和内置模块。webpack是运行在node环境中,在学习vue-cli的webpack配置的时候,
发现有的文件模块:
exports.fun1=function(param){ // }
exports.fun2=function(param){ // },
比如utils.js文件。。。
而有的文件模块:
module.exports = { // }
他们有什么区别呢?我只是自己的理解与资料总结,会一直更新与更改:
- 1.每个
js文件一创建,都有一个var exports = module.exports = {};,使exports和module.exports都指向一个空对象。 - 2.
module是全局内置对象,exports是被var创建的局部对象。 - 3.
module.exports和exports所指向的内存地址相同
- 1.
module.exports像是exports的大哥,当module.exports以{}整体导出时会覆盖exports的属性和方法, - 2.注意,若只是将属性/方法挂载在
module.exports./exports.上时,exports.id=1和module.exports.id=100,module.exports.id=function(){}和exports.id=function(){},最后id的值取决于exports.id和module.exports.id的顺序,谁在后,就是最后的值
- 3.若
exports和module.exports同时赋值时,exports所使用的属性和方法必须出现在module.exports,若属性没有在module.exports中定义的话,出现undefined,若方法没有在module.exports中定义,会抛出TypeError错误。RHS查询
总之:如果只是单一属性或方法的话,就使用exports.属性/方法。要是导出多个属性或方法或使用对象构造方法,结合prototype等,就建议使用module.exports = {}。
待求证,这只是总结资料的。
exports与module.exports的区别的更多相关文章
- nodejs模块中exports和module.exports的区别
通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ ,这些核心模块被编译成二进制文件,可以require('模块名')去获取:核心 ...
- node exports和module.exports区别
我们只需知道三点即可知道 exports 和 module.exports 的区别了: exports 是指向的 module.exports 的引用 module.exports 初始值为一个空对象 ...
- exports和module.exports的区别
总结:exports是module.exports的指向. 1. module应该是require方法中,上下文中的对象 2. exports对象应该是上下文中引用module.exports的新对象 ...
- nodejs中exports与module.exports的区别详细介绍
如果模块是一个特定的类型就用Module.exports.如果模块是一个典型的"实例化对象"就用exports. exports.name = function() { conso ...
- exports与module.exports的区别,export与export.defult区别
在JS模块化编程中,之前使用的是require.js或者sea.js.随着前端工程化工具webpack的推出,使得前端js可以使用CommonJS模块标准或者使用ES6 moduel特性. 在Comm ...
- 【nodejs】exports 和 module.exports 的区别
require 用来加载代码,而 exports 和 module.exports 则用来导出代码.但很多新手可能会迷惑于 exports 和 module.exports 的区别,为了更好的理解 e ...
- exports和module.exports区别
参考:module.exports与exports的区别.关于exports的总结 exports 和 module.exports 的区别 module.exports是真正的模块接口,而expor ...
- exports与module.exports的区别,以及export与export.defult的区别
在 JS 模块化编程的模块引入上, 主要有两种方式: CommonJS 模块标准 ES6 moduel 特性 1. CommonJS 模块引入:require() 模块导出:exports 或者 mo ...
- Node.js中exports与module.exports的区别
原文:http://www.hacksparrow.com/node-js-exports-vs-module-exports.html 你肯定对Node.js模块中用来创建函数的exports对象很 ...
随机推荐
- CI cookie 存放数组
#ci cookie 由于不能存放数组,所有必须序列化之后在存入数组中 #定义数组 $cookie_array=array( 'shop_id'=>$gid, 'shop_name'=> ...
- mysql 8 安装及更改密码
一 下载ZIP版的安装文件, 二 解压缩至指定的目录,如:d:\mysql8.0 三 在所在目录下,新建mysql.ini文件 [mysql] # 设置mysql客户端默认字符集 default-ch ...
- mui 单页面下拉刷新
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- TypeScript 接口(三)
TypeScript的核心原则之一是对值所具有的结构进行类型检查. 接口初始: interface objProperty { name: string } function printName(na ...
- hdu 2074 叠筐 好有意思的绘图题
叠筐 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- hiho一下第109周《Tower Defense Game》
题目链接:传送门 题目大意:给你一棵树,根节点为1,树上每一个节点都有一个花费值和收入值(花费值>=收入值),要访问一个节点需先支付花费值,访问该节点结束后得到收入值 同时访问树时要求是有序的, ...
- Leetcode-Permuation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- mybatis循环map
一.循环key <foreach collection="map.keys" item="key" separator="and"&g ...
- POJ 3150 Cellular Automaton(矩阵快速幂)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3504 Accepted: 1421 C ...
- POJ 2773 Happy 2006(容斥原理+二分)
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10827 Accepted: 3764 Descr ...