【发布时间】:2016-06-18 11:14:58
【问题描述】:
我正在使用 Browserify 构建我的捆绑包。
我有以下service.js:
(function (exports, require) {
// ...
var Service = function (name, region) {
this.name = name;
this.region = region;
// ...
}
exports = Service;
})(module.exports, require);
每当我尝试在另一个模块上 require('./service') 时,我都会得到一个空对象,就好像从未设置过 exports 对象一样。
如果我在没有参数封装的情况下使用module.exports,一切正常:
(function (require) {
// ...
var Service = function (name, region) {
this.name = name;
this.region = region;
// ...
}
module.exports = Service;
})(require);
为什么会发生这种情况以及为什么需要这样做?
【问题讨论】:
-
为什么不简单地返回
Service并从 iife 的结果中分配exports?
标签: javascript browserify commonjs