最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

exports与module.exports使用方法

来源:动视网 责编:小采 时间:2020-11-27 19:54:26
文档

exports与module.exports使用方法

exports与module.exports使用方法:这次给大家带来exports与module.exports使用方法,exports与module.exports使用的注意事项有哪些,下面就是实战案例,一起来看一下。 1. exports 是 module.exports 的 辅助对象,exports对外提供api 时需要用return 返回exports 对
推荐度:
导读exports与module.exports使用方法:这次给大家带来exports与module.exports使用方法,exports与module.exports使用的注意事项有哪些,下面就是实战案例,一起来看一下。 1. exports 是 module.exports 的 辅助对象,exports对外提供api 时需要用return 返回exports 对


这次给大家带来exports与module.exports使用方法,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 是一个对象,用来向外提供模块接口。

define(function(require, exports) {
 // 对外提供 foo 属性
 exports.foo = 'bar';
 // 对外提供 doSomething 方法
 exports.doSomething = function() {};
});

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

define(function(require) {
 // 通过 return 直接提供接口
 return {
 foo: 'bar',
 doSomething: function() {}
 };
});

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

define({
 foo: 'bar',
 doSomething: function() {}
});

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

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

define(function(require, exports) {
 // 错误用法!!!
 exports = {
 foo: 'bar',
 doSomething: function() {}
 };
});

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

define(function(require, exports, module) {
 // 正确写法
 module.exports = {
 foo: 'bar',
 doSomething: function() {}
 };
});

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

module.exports Object

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

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

define(function(require, exports, module) {
 // exports 是 module.exports 的一个引用
 console.log(module.exports === exports); // true
 // 重新给 module.exports 赋值
 module.exports = new SomeClass();
 // exports 不再等于 module.exports
 console.log(module.exports === exports); // false
});

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

// x.jsdefine(function(require, exports, module) {
 // 错误用法
 setTimeout(function() {
 module.exports = { a: "hello" };
 }, 0);
});

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

// y.jsdefine(function(require, exports, module) {
 var x = require('./x');
 // 无法立刻得到模块 x 的属性 a
 console.log(x.a); // undefined
});

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

vue中数组变化检测问题

JS上传文件时显示进度条

文档

exports与module.exports使用方法

exports与module.exports使用方法:这次给大家带来exports与module.exports使用方法,exports与module.exports使用的注意事项有哪些,下面就是实战案例,一起来看一下。 1. exports 是 module.exports 的 辅助对象,exports对外提供api 时需要用return 返回exports 对
推荐度:
标签: 方法 使用 用法
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top