exports 与 module.exports 的区别
exports与module.exports的作用就是将方法或者是变量暴露出去,以便给其他模块调用,再直接点,就是给其他模块通过require()的方式引用。
那么require()一个模块时,到底做了什么呢?下面大概展现了require()的伪代码实现:
function require(/* ... */) {
const module = { exports: {} };
((module, exports) => {
// Module code here. In this example, define a function.
// function someFunc() {}
// exports = someFunc;
// At this point, exports is no longer a shortcut to module.exports, and
// this module will still export an empty default object.
// module.exports = someFunc;
// At this point, the module will now export someFunc, instead of the
// default object.
})(module, module.exports);
return module.exports;
}
由此可以看出:
- module.exports默认指向一个空对象{}
- exports在默认情况下是指向module.exports的引用
- require()返回的是module.exports而不是exports
所以,如果是通过exports.test = function() {} 的形式暴露出去,无论是使用exports还是使用module.exports,在功能上并没有什么差别;但是如果是通过exports = function() {} 的形式暴露出去,结果往往会出乎意料。从伪代码中可知,exports对象是通过形参的方式传入的,直接赋值形参会改变形参的引用,但是不会改变作用域外的值,也就是说module.exports原来的值是什么,现在还是什么,不会有任何改变,而require()方法最终返回的是module.exports,所以结果可想而知。
// 文件1.js
exports = function () {
console.log('exports')
} // 文件2.js
var test = require('./1');
console.log(test); // 结果是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的区别
nodejs有自己的模块系统,分为文件模块和内置模块.webpack是运行在node环境中,在学习vue-cli的webpack配置的时候, 发现有的文件模块: exports.fun1=functi ...
- 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对象很 ...
随机推荐
- poj 2406 Power Strings (后缀数组 || KMP)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 28859 Accepted: 12045 D ...
- [UOJ#348][WC2018]州区划分
[UOJ#348][WC2018]州区划分 试题描述 小 \(S\) 现在拥有 \(n\) 座城市,第ii座城市的人口为 \(w_i\),城市与城市之间可能有双向道路相连. 现在小 \(S\) 要将这 ...
- 【马克-to-win】学习笔记—— 第五章 异常Exception
第五章 异常Exception [学习笔记] [参考:JDK中文(类 Exception)] java.lang.Object java.lang.Throwable java.lang.Except ...
- bzoj 4621 Tc605 思想+dp
4621: Tc605 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 328 Solved: 183[Submit][Status][Discuss ...
- 适配器模式 & 装饰器模式
一.适配器模式:简单来讲,就是为了方便使用,完成从 一个接口 到 另一个接口 的 转换,这个负责转换的就是 适配器例如:Reader ——> InputStreamReader 通过类内部组合 ...
- Javascript时间差计算函数代码实例
Javascript时间差计算函数代码实例 <script language="javascript"> Date.prototype.dateDiff = funct ...
- hihoCoder挑战赛34 B题(快速求第k轮冒泡排序的结果)
官方题解:https://media.hihocoder.com/contests/challenge34/tutorials-previewed.pdf 题目链接:http://hihocoder. ...
- 转载:GCC 提供的原子操作
转载自:GCC 提供的原子操作 GCC 提供的原子操作 gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作. 其声明如下: type __sync_f ...
- java应用挂死故障排查
现象: java开发的web应用无法访问 排查: 1.从resin/log/watchdog-manager.log的日志里可以看出来,jvm的内存满,无法创建新进程 java.lang.OutOfM ...
- 解决:高版本jdk编译低版本代码时eclipse提示Access restriction:The type 'Unsafe' is not accessible due to restriction on required library
在Eclipse中采用高版本jdk编译一些低版本的源码时,由于源码中使用了一些高版本中过时的API,可能就会报错,类似于: Access restriction:The type 'Unsafe' i ...