为什么在定义导出属性之前调用module.exports时仍然有效
Why does module.exports still work when called before defining exported properties?
我有这个代码:
var config = module.exports = {};
config.foo = {...};
这个作品找到了,但我想知道为什么。
我觉得我可以考虑下面的实现,因为导出配置似乎更有意义,而不是将配置设置为module.exports.
var config = {};
config.foo = {...};
module.exports = config;
有人能解释一下吗?
var config = module.exports = {};
相当于
var config = (module.exports = {});
和
module.exports = {};
var config = module.exports;
此处导出的值为{}
。因为config
和module.exports
只是对同一对象{}
的引用,所以属性foo
仍然可以通过变量module.exports
或config
添加到该对象。
相关文章:
- 为什么要对define.md和module.exports进行条件检查,以及条件何时通过
- 在jasmine规范中使用module.exports
- 我们可以在一个js文件中有多个module.exports语句吗
- module.exports一个jQuery插件
- “module.exports.instance”是什么意思
- Module.exports通过Mongoose调用返回undefined
- 我可以在Node中以编程方式更改.js文件的module.exports中的JSON对象吗
- 使用module.exports和ES6导出导入
- 调用module.exports上的函数时发生CommonJS非法调用错误
- 如何从module.exports访问函数
- 为什么在定义导出属性之前调用module.exports时仍然有效
- “var self = module.exports;”是什么意思
- this.prototype 和 module.exports 之間的差異
- 我们如何在nodeJS中使用module.exports返回函数
- 在定义foo之前放置“module.exports = foo”是如何工作的
- module.exports scope and node-mysql
- require():使用 module.exports vs 直接分配给 “this”
- Node.js/MongoDB:如何使用module.exports将本地主机数据库URL传递给服务器.js文件
- NodeJS prototyping with module.exports
- Expressjs JavaScript Fundamentals: exports = module.exports