当前位置:Gxlcms > JavaScript > seaJs中关于exports和module.exports的区别详解

seaJs中关于exports和module.exports的区别详解

时间:2021-07-01 10:21:17 帮助过:6人阅读

这篇文章主要介绍了seaJs使用心得之exports与module.exports的区别,结合实例形式分析了exports与module.exports具体功能、使用方法及相关操作注意事项,需要的朋友可以参考下

本文实例讲述了seaJs使用心得之exports与module.exports的区别。分享给大家供大家参考,具体如下:

1. exports 是 module.exports 的 辅助对象,exports对外提供api 时需要用return 返回exports 对象

2. module.exports 也可直接向外提供api

参考 : https://github.com/seajs/seajs/issues/242

exports Object

exports 是一个对象,用来向外提供模块接口。


除了给 exports 对象增加成员,还可以使用 return 直接向外提供接口。


如果 return 语句是模块中的唯一代码,还可简化为:


上面这种格式特别适合定义 JSONP 模块。

特别注意:下面这种写法是错误的!


正确的写法是用 return 或者给 module.exports 赋值:


提示:exports 仅仅是 module.exports 的一个引用。在 factory 内部给 exports 重新赋值时,并不会改变 module.exports 的值。因此给 exports 赋值是无效的,不能用来更改模块接口。

module.exports Object

当前模块对外提供的接口。

传给 factory 构造方法的 exports 参数是 module.exports 对象的一个引用。只通过 exports 参数来提供接口,有时无法满足开发者的所有需求。 比如当模块的接口是某个类的实例时,需要通过 module.exports来实现:


注意:对 module.exports 的赋值需要同步执行,不能放在回调函数里。下面这样是不行的:


在 y.js 里有调用到上面的 x.js:


以上就是seaJs中关于exports和module.exports的区别详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行