javascript - 你应该总是 `module.export` 一个 JS 模块吗?

标签 javascript

我最近在几个地方看到了以下内容,想知道为什么您需要导出一个空对象?

module.exports = {};

例如,模块是一个 polyfill,只包含一个自执行函数。最后的 module.exports = {} 有什么作用?因为代码似乎在有或没有它的情况下都可以工作(我可以要求模块作为依赖项)。

(function polyfillWindowLocationOrigin (location) {
    if (location.origin === undefined) {
        location.origin = location.protocol + '//' + location.host;
    }
})(window.location);

module.exports = {};

注意:我们正在使用 browserify。

最佳答案

如果使用 CommonJS 模块系统,则可以假定此代码出现在每个模块的开头:

var module = {
    exports: {}
};

因此设置 module.exports = {}; 在技术上完成的很少,但在明确记录模块不导出任何内容这一事实方面可能更可取。

关于javascript - 你应该总是 `module.export` 一个 JS 模块吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37790725/

相关文章:

javascript - RaphaelJS 中对象的渐变透明度

javascript - 在 Vue.js 3 中搜索 react 数组

javascript - 如何使用类型脚本在 angular2 中对格式进行编号?

javascript - 如何复制部分文本并使用 Jquery 填充输入

javascript - img.width 和 img.height 随机返回 0

javascript - 尝试编写一个依赖于两个不同连接表的 'or' 查询

javascript - 解决函数未注入(inject)到 Controller 中的问题 [Angular JS]

javascript - 读取包含整个 html 代码的 jquery 变量中标签的属性时出现问题

javascript - 输入时读取登录凭据中的计时器的辅助功能 (ADA) 问题

javascript - ionic 框架 : Can't redirect to another page from my controller?