详解Sea.js中Module.exports和exports的区别

 更新时间:2017年02月12日 11:42:13   作者:信鑫  
最近在看Seajs时,看到了exports.doSomething和module.exports,想对这两者的区别一探究竟。所以下面这篇文章主要介绍了Sea.js中Module.exports和exports的区别,需要的朋友可以参考借鉴,一起来看看吧。
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

一、官方解释

因为SeaJs和Nodejs都是基于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;
}

二、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);

从事例一中,可以看出来

      1.每个js文件一创建,都有一个var exports = module.exports = {}; ,使exportsmodule.exports都指向一个空对象。

      2.module是全局内置对象,exports是被var创建的局部对象。

      3.module.exportsexports所指向的内存地址相同

事例二:2.js、3.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的函数');
  }

};
// 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.js、5.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
// 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 = {} 。文章有很多地方描述的可能不是很准确,提到的点也不够全面,如果有不对的地方,还望斧正!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

相关文章

  • seajs学习教程之基础篇

    seajs学习教程之基础篇

    Seajs是一个Web模块加载框架,追求简单、自然的代码书写和组织方式,Sea.js 遵循 CMD 规范,模块化JS代码。依赖的自动加载、配置的简洁清晰,可以让程序员更多地专注编码。这篇文章为seajs深入学习的第一篇,主要介绍的基础性的内容,感兴趣的朋友们可以参考学习。
    2016-10-10
  • Vue实现图片懒加载的多种方法详解

    Vue实现图片懒加载的多种方法详解

    图片懒加载是前端开发中一项重要的性能优化策略,它能够明显缩短页面加载时间,提升用户的浏览体验,在本文中,我们将深入探讨在Vue开发时候如何实现图片懒加载,同时探讨多种实现途径,需要的朋友可以参考下
    2023-08-08
  • seajs模块压缩问题与解决方法实例分析

    seajs模块压缩问题与解决方法实例分析

    这篇文章主要介绍了seajs模块压缩问题与解决方法,结合实例形式分析了seajs模块压缩过程中出现的问题及相应的解决方法,需要的朋友可以参考下
    2017-10-10
  • SeaJS中use函数用法实例分析

    SeaJS中use函数用法实例分析

    这篇文章主要介绍了SeaJS中use函数用法,结合实例形式分析了use函数加载模块的使用方法与相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • seajs中模块的解析规则详解和模块使用总结

    seajs中模块的解析规则详解和模块使用总结

    这篇文章主要介绍了seajs中模块的解析规则详解和模块使用总结,需要的朋友可以参考下
    2014-03-03
  • seajs实现强制刷新本地缓存的方法分析

    seajs实现强制刷新本地缓存的方法分析

    这篇文章主要介绍了seajs实现强制刷新本地缓存的方法,结合实例形式简单分析了seajs强制刷新本地缓存的原理、实现方法及相关注意事项,需要的朋友可以参考下
    2017-10-10
  • seajs学习之模块的依赖加载及模块API的导出

    seajs学习之模块的依赖加载及模块API的导出

    SeaJS是一个遵循 CommonJS 规范的模块加载框架,可用来轻松愉悦地加载任意JavaScript模块和css模块样式。SeaJS接口和方法也非常少,SeaJS 就两个核心:模块定义和模块的加载及依赖关系。本文将详细介绍模块的依赖加载及模块API的导出,有需要的朋友们可以参考借鉴。
    2016-10-10
  • SeaJS入门教程系列之SeaJS介绍(一)

    SeaJS入门教程系列之SeaJS介绍(一)

    这篇文章主要介绍了SeaJS入门教程,讲述了SeaJS的由来,JavaScript传统开发模式和模块化开发的对比,需要的朋友可以参考下
    2014-03-03
  • Seajs是什么及sea.js 由来,特点以及优势

    Seajs是什么及sea.js 由来,特点以及优势

    这篇文章主要介绍了Seajs的相关知识和和学习心得,适合刚接触SeaJS的同学,需要的朋友可以参考下,有更好的新手教程或文档,欢迎推荐、分享
    2016-10-10
  • seaJs的模块定义和模块加载浅析

    seaJs的模块定义和模块加载浅析

    这篇文章主要介绍了seaJs的模块定义和模块加载浅析,以及模块间的依赖关系等问题,需要的朋友可以参考下
    2014-06-06

最新评论

?


http://www.vxiaotou.com