Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Module.exports和exports的区别

Module.exports和exports的区别

原创
作者头像
金朝麟
修改于 2017-06-19 11:26:48
修改于 2017-06-19 11:26:48
1.3K0
举报
文章被收录于专栏:金朝麟的专栏金朝麟的专栏

学习Seajs时,看到了exports.doSomethingmodule.exports,想对这两者的区别一探究竟。

一、官方解释

因为SeaJsNodejs都是基于CommonJS,所以直接看的Node的官方文档解释

Module.exports

The module.exports object is created by the Module system. Sometimes this is not acceptable; many want their module to be an instance of some class. To do this, assign the desired export object to module.exports. Note that assigning the desired object to exports will simply rebind the local exports variable, which is probably not what you want to do.

module.exports对象是由模块系统创建的。 有时这是难以接受的;许多人希望他们的模块成为某个类的实例。 为了实现这个,需要将期望导出的对象赋值给module.exports。 注意,将期望的对象赋值给exports会简单地重新绑定到本地exports变量上,这可能不是你想要的。

exports

The exports variable is available within a module's file-level scope, and is assigned the value of module.exports before the module is evaluated. It allows a shortcut, so that module.exports.f = ... can be written more succinctly as exports.f = .... However, be aware that like any variable, if a new value is assigned to exports, it is no longer bound to module.exports:

译文:exports变量是在模块的文件级别作用域内有效的,它在模块被执行前被赋于 module.exports 的值。它有一个快捷方式,以便 module.exports.f = ... 可以被更简洁地写成exports.f = ...。 注意,就像任何变量,如果一个新的值被赋值给exports,它就不再绑定到module.exports(其实是exports.属性会自动挂载到没有命名冲突的module.exports.属性)

require

从require导入方式去理解,关键有两个变量(全局变量module.exports,局部变量exports)、一个返回值(module.exports)

代码语言:js
AI代码解释
复制
function require(...) {
  var module = { exports: {} };
  ((module, exports) => {
    // 你的被引入代码 Start
    // var exports = module.exports = {}; (默认都有的)
    function some_func() {};
    exports = some_func;
    // 此时,exports不再挂载到module.exports,
    // export将导出{}默认对象
    module.exports = some_func;
    // 此时,这个模块将导出some_func对象,覆盖exports上的some_func    
     // 你的被引入代码 End
  })(module, module.exports);
 // 不管是exports还是module.exports,最后返回的还是module.exports 
  return module.exports;
}

二、Demo事例

事例一:1.js

代码语言:js
AI代码解释
复制
console.log(exports); // {}
console.log(module.exports);  // {}
console.log(exports === module.exports);	// true
console.log(exports == module.exports);		// true
/**
 Module {
  id: '.',
  exports: {},
  parent: null,
  filename: '/1.js',
  loaded: false,
  children: [],
  paths:
   [ 
     '/node_modules' ] 
 }
 */
console.log(module);

从事例一中,可以看出来

  • 1.每个js文件一创建,都有一个var exports = module.exports = {};,使exportsmodule.exports都指向一个空对象。
  • 2.module是全局内置对象,exports是被var创建的局部对象。
  • 3.module.exportsexports所指向的内存地址相同

事例二:2.js3.js

代码语言:js
AI代码解释
复制
// 2.js
exports.id = 'exports的id';
exports.id2 = 'exports的id2';
exports.func = function(){
	console.log('exports的函数');
};
exports.func2 = function() {
	console.log('exports的函数2');
};
module.exports = {
	id: 'module.exports的id',
	func:function(){
		console.log('module.exports的函数');
	}

};

代码语言:js
AI代码解释
复制
// 3.js
var a = require('./2.js');
// 当属性和函数在module.exports都有定义时:
console.log(a.id);  // module.exports的id
console.log(a.func()); // module.exports的函数

// 当属性在module.exports没有定义,函数在module.exports有定义
console.log(a.id2);  // undefined
console.log(a.func());  // module.exports的函数

// 当函数在module.exports没有定义,属性在module.exports有定义
console.log(a.id);		// module.exports的id
console.log(a.func2());	// 报错了 TypeError: a.func2 is not a function

由例二可以知道:

  • 1.module.exports像是exports的大哥,当module.exports{}整体导出时会覆盖exports的属性和方法,
  • 2.注意,若只是将属性/方法挂载在module.exports./exports.上时,exports.id=1module.exports.id=100module.exports.id=function(){}exports.id=function(){},最后id的值取决于exports.idmodule.exports.id的顺序,谁在后,就是最后的值

  • 3.若exportsmodule.exports同时赋值时,exports所使用的属性和方法必须出现在module.exports,若属性没有在module.exports中定义的话,出现undefined,若方法没有在module.exports中定义,会抛出TypeError错误。

例三 4.js5.js

module.exports的对象、prototype构造函数使用

代码语言:js
AI代码解释
复制
// 4.js
var a = require('./5.js');
// 若传的是类,new一个对象
var person = new a('Kylin',20);
console.log(person.speak()); // my name is Kylin ,my age is 20

// 若不需要在构造函数时初始化参数,直接调用方法/属性
// a.speak();  // my name is kylin ,my age is 20

代码语言:js
AI代码解释
复制
// 5.js
// Person类
function Person(name,age){
	this.name = name;
	this.age = age;
}
// 为类添加方法
Person.prototype.speak = function(){
	console.log('my name is '+this.name+' ,my age is '+this.age);
};

// 返回类
module.exports = Person;

// 若构造函数没有传入参数(name,age),直接传入对象
// module.exports = new Person('kylin',20);

说了这么多,其实建议就是,如果只是单一属性或方法的话,就使用exports.属性/方法。要是导出多个属性或方法或使用对象构造方法,结合prototype等,就建议使用module.exports = {}。文章有很多地方描述的可能不是很准确,提到的点也不够全面,如果有不对的地方,还望斧正!

参考资料:Nodejs官方文档(中文)Node.js Module – exports vs module.exportsUnderstanding module.exports and exports in Node.jsexports 和 module.exports 的区别

[

](https://www.qcloud.com/act/newuser?fromSource=gwzcw.93775.93775.93775)

相关推荐

前端开发框架简介:angular和react
包学会之浅入浅出Vue.js:开学篇

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
你真的了解esModule吗
项目中我们常常会接触到模块,最为典型代表的是esModule与commonjs,在es6之前还有AMD代表的seajs,requirejs,在项目模块加载的文件之间,我们如何选择,比如常常因为某个变量,我们需要动态加载某个文件,因此你想到了require('xxx'),我们也常常会用import方式导入路由组件或者文件,等等。因此我们有必要真正明白如何使用好它,并正确的用好它们。
Maic
2022/07/28
4420
你真的了解esModule吗
Node.js Module – exports 和module.exports之间的联系与区别
http://www.hacksparrow.com/node-js-exports-vs-module-exports.html 翻译 相信大家都很熟悉 exports 的用法了,你可以创建一个 function 在你的模块中如下:
蛋未明
2022/05/09
7690
module.exports和exports,应该用哪个
在这篇文章中,我将介绍如何在 Node.js 中使用模块,重点是如何导出和消费它们。
chuckQu
2023/09/12
2380
module.exports和exports,应该用哪个
exports和module.exports介绍
为了让nodejs开发过程中,为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统,模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的。换言之,一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码、JSON 或者编译过的C/C++ 扩展。
OECOM
2020/07/01
1.6K0
NodeJs-Lesson1-require和module.exports探究
require和module.exports探究 require和module.exports讲解 require exports和module.exports探究 留一个疑问?为什么exports和require都能直接使用,是全局变量吗? require和module.exports讲解 遵守开闭原则:对修改关闭,对扩展开放,所以要想使用引入的模块,子模块必须提供module.exports方法。否则只能运行子模块 require 使用方法 var express = require(“expres
envoke
2020/09/17
5630
NodeJs-Lesson1-require和module.exports探究
js模块化编程之彻底弄懂CommonJS和AMD/CMD!(转)
答:因为有了模块,我们就可以更方便地使用别人的代码,想要什么功能,就加载什么模块。 但是,这样做有一个前提,那就是大家必须以同样的方式编写模块,否则你有你的写法,我有我的写法,岂不是乱了套!
jojo
2019/03/12
1.7K0
前端工程师常考手写面试题指南
函数柯里化概念: 柯里化(Currying)是把接受多个参数的函数转变为接受一个单一参数的函数,并且返回接受余下的参数且返回结果的新函数的技术。
helloworld1024
2022/12/07
4760
require时,exports和module.exports的区别你真的懂吗?
提到 exports 和 module.exports 我们不得不提到 require 关键字。大家哦读知道 Node.js 遵循 CommonJS 规范,使用 require 关键字来加载模块。
coder_koala
2019/08/19
1.7K0
require时,exports和module.exports的区别你真的懂吗?
CommonJS与ES6 Module的本质区别
动态与静态CommonJS与ES6 Module最本质的区别在于前者对模块依赖的解决是“动态的”,而后者是“静态的”。在这里“动态”的含义是,模块依赖关系的建立发生在代码运行阶段;而“静态”则是模块依赖关系的建立发生在代码编译阶段。
砖业洋__
2023/05/06
3900
exports 和 module.exports 的区别
https://cnodejs.org/topic/5231a630101e574521e45ef8
bear_fish
2018/09/19
7250
what is 模块化?
将一个复杂的程序按照一定的规范,封装成几个块(文件),并进行组合在一起。 这些模块,最好都做到可复用性,比如可以在多个文件中使用处理时间的模块。
用户4793865
2023/01/12
1.2K0
what is 模块化?
node.js中exports与module.exports的区别
node.js中,每个js文件都可以当成一个模块,每个模块中,都隐含了一个名为module的对象,module对象中有一个exports属性,这个属性的功能是将模块中的变量暴露给其他模块调用。
章鱼喵
2018/08/02
1K0
node.js中exports与module.exports的区别
前端模块化
js本身的问题: 不具有模块化的语法规则,在语言层面没有命名空间。 JavaScript 编程过程中很多时候,我们都在修改变量,在一个复杂的项目开发过程中,如何管理函数和变量作用域,显得尤为重要。
前端小tips
2021/12/10
5080
前端模块化
NodeJs当中module.exports和exports的区别
Module.exports才是真正的接口,exports只不过是它的一个辅助工具。 最终返回给调用的是Module.exports而不是exports。 所有的exports收集到的属性和方法,都赋值给了Module.exports。当然,这有个前提,就是Module.exports本身不具备任何属性和方法。如果,Module.exports已经具备一些属性和方法,那么exports收集来的信息将被忽略。 如果你想你的模块是一个特定的类型就用Module.exports。如果你想的模块是一个典型的“实例化
苦咖啡
2018/04/28
1.1K0
module.exports 、exports、export、export default的区别
module.exports和exports是属于 CommonJS 模块规范,export和export default是属于ES6语法。
wfaceboss
2019/04/08
9.4K0
模块化编程之require.js
最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了。后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载。下面的网页代码,相信很多人都见过。  
jojo
2022/03/21
1.7K0
exports 和 module.exports 的区别
require 用来加载代码,而 exports 和 module.exports 则用来导出代码。但很多新手可能会迷惑于 exports 和 module.exports 的区别,为了更好的理解 exports 和 module.exports 的关系,我们先来巩固下 js 的基础。示例: test.js var a = {name: 1}; var b = a; console.log(a); console.log(b); b.name = 2; console.log(a); console.log
吴裕超
2018/02/28
1.1K0
exports 和 module.exports 的区别
聊聊CommonJS与ES6 Module的使用与区别
学了JS并且用过Node.js后,对模块化应该是有所了解和使用了,那么一定见过以下两种模块导入导出的方式
前端迷
2020/11/23
1.4K0
Node 导入模块:require()和导出模块:module.exports、exports的用法及注意点
注意:require()导入模块时,得到的永远是module.exports指向的对象
倾盖
2022/08/16
1.3K0
前端模块化杂记 前言AMD/CMD简介Commonjs简介Module简介Common和Module的区别Module与webpackModule与Babel一些问题总结引用
前端模块化在近几年层出不穷,有Node的CommonJs,也有属于client端的CMD/AMD模式,而ES6本身也出现了Modules,再加上Webpack以及babel的普及,虽然在代码中经常使用到这些用法,但是如果不去深入研究,总觉得是一个黑魔法,无法探测一些问题的根源。
菜的黑人牙膏
2019/01/28
9010
相关推荐
你真的了解esModule吗
更多 >
LV.0
这个人很懒,什么都没有留下~
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档