Javascript node.js模块导出

Javascript node.js模块导出,javascript,node.js,commonjs,Javascript,Node.js,Commonjs,这样做的原因是什么: exports.foo = 'foo'; var bar = require('./foo'); console.log(bar); // {foo: 'foo'} 但这并不是: var data = { foo: 'foo' }; exports = data; var bar = require('./foo'); console.log(bar); // {} // expected {foo: 'foo'} 在第二段代码中,基本上覆盖了导出对象。所以,即使你

这样做的原因是什么:

exports.foo = 'foo';

var bar = require('./foo');
console.log(bar); // {foo: 'foo'}
但这并不是:

var data = { foo: 'foo' };
exports = data;

var bar = require('./foo');
console.log(bar); // {}
// expected {foo: 'foo'}

在第二段代码中,基本上覆盖了导出对象。所以,即使你的代码可以工作,我想所有其他的导出都会被它破坏(覆盖)。因此,节点可能有一些保护机制来避免这种情况

好吧,在第二段代码中,您基本上覆盖了导出对象。所以,即使你的代码可以工作,我想所有其他的导出都会被它破坏(覆盖)。因此,节点可能有一些保护机制来避免这种情况

您可以通过替换
exports=data来修复第二个代码带有
module.exports=数据


前者不起作用的原因是它只分配给模块名称空间中的另一个对象。后者将
模块
对象上的
导出
属性值替换为
数据
对象。

您可以通过替换
导出=数据来修复第二个代码带有
module.exports=数据


前者不起作用的原因是它只分配给模块名称空间中的另一个对象。后者将
模块
对象上的
导出
属性的值替换为
数据
对象。

我将尝试以javascript问题的形式回答这个问题 代码示例

function a() {}
a.prototype.foo = {test:"bar"}
var d = new a();
var c = new a();
console.log(d.prototype ==== c.prototype) // Both c and d share the same prototype object
d.foo.hai = "hello"
console.log(d.prototype ==== c.prototype) // Still the they refer to the same
d.foo = {im: "sorry"}
console.log(d.prototype ==== c.prototype) // now they don't
节点也一样

console.log(module.exports === exports);// true; They refer to the same object
exports.a = {tamil: "selvan"} 
console.log(module.exports === exports);// true even now 

exports = {sorry: "im leaving"}; // overrides modules.exports
console.log(module.exports === exports); //false now they don't
console.log(module.exports); // {a: {tamil: "selvan"}}
console.log(exports);  // {sorry: "im leaving"}
导出和module.exports引用同一个核心对象,直到您覆盖了与原型对象相同的核心对象。覆盖参照更改的那一刻

module.exports={something:“works”}
之所以有效,是因为在缓存节点所关心的模块时,您正在更改该模块的属性

即使是上述情况

module.exports===exports//为false它们不再相同

这证明反之亦然:)


还有一件事
模块
是对当前模块的引用,因此总是更喜欢使用
模块。导出
而不是
导出
我将尝试以javascript问题的形式回答这个问题 代码示例

function a() {}
a.prototype.foo = {test:"bar"}
var d = new a();
var c = new a();
console.log(d.prototype ==== c.prototype) // Both c and d share the same prototype object
d.foo.hai = "hello"
console.log(d.prototype ==== c.prototype) // Still the they refer to the same
d.foo = {im: "sorry"}
console.log(d.prototype ==== c.prototype) // now they don't
节点也一样

console.log(module.exports === exports);// true; They refer to the same object
exports.a = {tamil: "selvan"} 
console.log(module.exports === exports);// true even now 

exports = {sorry: "im leaving"}; // overrides modules.exports
console.log(module.exports === exports); //false now they don't
console.log(module.exports); // {a: {tamil: "selvan"}}
console.log(exports);  // {sorry: "im leaving"}
导出和module.exports引用同一个核心对象,直到您覆盖了与原型对象相同的核心对象。覆盖参照更改的那一刻

module.exports={something:“works”}
之所以有效,是因为在缓存节点所关心的模块时,您正在更改该模块的属性

即使是上述情况

module.exports===exports//为false它们不再相同

这证明反之亦然:)

还有一件事
module
是对当前模块的引用,因此总是喜欢使用
module.exports
而不是
exports