Module.exports和exports的区别

原文链接: https://www.ycjcl.cc/2017/02/10/module-exportshe-exportsde-qu-bie/

学习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)

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;
}

Copy

二、Demo事例

事例一:1.js
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);

Copy

从事例一中,可以看出来
  • 1.每个js文件一创建,都有一个var exports = module.exports = {};,使exportsmodule.exports都指向一个空对象。
  • 2.module是全局内置对象,exports是被var创建的局部对象。
  • 3.module.exportsexports所指向的内存地址相同
事例二:2.js3.js
// 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的函数‘);
    }

};

Copy

// 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

Copy

由例二可以知道:
  • 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构造函数使用

// 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

Copy

// 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);

Copy

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

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

时间: 2024-08-06 20:02:27

Module.exports和exports的区别的相关文章

【原创】module.exports和exports export和export default import和require区别与联系一次性解决

还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: module.exports和exports是属于CommonJS模块规范!(不清楚commonjs?大神请这边逛一逛commonjs规范) export和export default是属于ES6语法(不清楚ES6?大神请这边逛一逛commonjs规范)! 同样import和require分别属于ES6和C

module.exports和exports得区别

对module.exports和exports的一些理解 可能是有史以来最简单通俗易懂的有关Module.exports和exports区别的文章了. exports = module.exports = {}; 所以module.exports和exports的区别就是var a={}; var b=a;,a和b的区别 看起来木有什么太大区别,但实际用起来的时候却又有区别,这是为啥呢,请听我细细道来 关于Module.exports和exports有什么区别,网上一搜一大把,但是说的都太复杂了

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规范规定,

Node.js module.exports和exports的区别

require 用来加载代码,而 exports 和 module.exports 则用来导出代码,从接触node.js就不会它们两陌生,上代码: foo.js exports.a = function(){ console.log('a') } exports.a = 1 test.js var x = require('./foo'); console.log(x.a) //1 foo.js exports.a = function(){ console.log('a') } module.

module.exports 与 exports的区别

这个问题近半年看过三次,一直都是迷迷糊糊的,今天看朴大以及一些stack上面的资料终于算是明白点了,先记录一下以免忘记. 大白话: exports 只是 module.exports 的引用,他们都指向内存中的同一个地址. node在加载模块的时候,每个模块默认有一个module对象,该对象有个属性exports;初始值是空对象{},可以认为require引入模块调用的是module.exports最终指向的对象. —-那么怎么理解exports只是module.exports的引用呢.可以这样

Node中Exports与module.export的使用与区别

最近在看<node开发实战详解>时有写疑问,所以自己就整理了一些资料.下面是node4.*的官方api文档(http://nodejs.cn/doc/node_4/modules.html#modules_module_exports),我有点看不懂,就拉出node.10*的官方api(https://nodejs.org/dist/v0.10.9/docs/api/modules.html#modules_module_exports). module.exports与exports的介绍

随笔一:module.exports 和 exports 的区别

1. node 中的模块遵循 CommonJs ,模块标识(module),模块定义(exports),模块引用(require) 2. module.exports 和 exports 指向内容中的同一块空间 3. exports 只是一个引用,如果 exports 被指向别的内存空间,导出东西依然不变.导出的东西就是 module.exports 参考链接:https://juejin.im/post/597ec55a51882556a234fcef exports  ['eksp?:rts

module.exports、exports、export、import、export default区别

CommonJs规范:module.exports.exports module.exports和exports 是commonJs的语法,大家熟知的node就是基于CommonJs语法设计的,node将每个文件视为一个拥有独立作用域链的模块, 每个木块的类,变量,函数等都是私有的,对其他文件不可见.但是,如果别的模块想要使用另一个模块的函数应该如何处理呢,这时我们就会用到module.exports.exports了. node将每个独立的文件视为一个mudule,而exports是为了将本模

module.exports与exports,export与export default的区别

Node使用CommonJS规范,定义每个模块的内部,module变量代表当前模块,exports是module的属性,表示对外的接口.加载某个模块,实际上是加载该模块的module.exports属性.Node为每隔模块提供了一个exports变量,指向module.exports,这等同于每个模块头部有这样的一行代码:var exports = module.exportsES6使用export和import来导出/导入模块.3.1 export与export default均可用于导出常量