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对象很 ...
随机推荐
- 微信小程序,时间戳转为日期格式
//数据转化 function formatNumber(n) { n = n.toString() ] ? n : ' + n } /** * 时间戳转化为年 月 日 时 分 秒 * number: ...
- 为什么需要标准IO缓冲?
(转)标准I/O缓冲:全缓冲.行缓冲.无缓冲 标准I/O库提供缓冲的目的是尽可能地减少使用read和write调用的次数.它也对每个I/O流自动地进行缓冲管理,从而避免了应用程序需要考虑这一点所带来的 ...
- html5 webwork
在Web开发的时候经常会遇到浏览器不响应事件进入假死状态,甚至弹出“脚本运行时间过长“的提示框,如果出现这种情况说明你的脚本已经失控了. 一个浏览器至少存在三个线程:js引擎线程(处理js).GUI渲 ...
- node读写Excel操作
目支持写Excel的node.js模块: node-xlsx: 基于Node.js解析excel文件数据及生成excel文件: excel-parser: 基于Node.js解析excel文件数据,支 ...
- jQuery ajax 动态append创建表格出现不兼容ie8
非常多情况下.通过js(jQuery1.10)动态来创建一些样式,对页面的动态交互来说是非常方便的 可是不同的浏览器针对动态生成的不是非常兼容,在此遇见的不兼容ie8.跟各位分享下 代码: json数 ...
- JavaScript------获取表单信息
<form name="fname"> <input type="text" name="user" /> < ...
- python bottle学习(三)动态路由配置(通配符)
from bottle import (run, route, get, post, default_app, Bottle) @route('/', method='GET') @route('/i ...
- CSS-微信开放UI样式
下面的链接是微信开放的CSS的样式: http://weui.github.io/weui/ 附上GitHub地址:https://github.com/weui/weui
- 亲测Google开源JPEG图片压缩算法,可将JPEG文件缩小%35
具体方法如下(windows 平台) GitHub上下载guetzli源码,地址https://github.com/google/guetzli GitHub上下载 vcpkg源码,地址https: ...
- iOS 断当前时间是否在一天的某个时间段内。
应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内.难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟 ...