English 中文(简体)
出口单元
原标题:node.js module exports

这样做的理由是什么:

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 }
问题回答

I ll Try to answer this as a javascript question Code Sample

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

no

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"}

出口和模块。 出口指相同的核心物体,直至您高于javsacript原型物体。 此时此刻,你推翻了参考变化。

module.exports = {something: "works"} works because you are changing the property of module that node cares while caching it.

即便如此,

module. Exports == export /is false they are no more same

这证明,反之亦然:

<代码>module系指经常模块,因此总是倾向于使用<代码>module.exports/code>而不是exports

您可以通过替换<代码>出口=数据;,改为module.exports = 数据;

The reason the former doesn t work is that it only assigns to exports another object in the module name space. While the latter replaces the value of exports property on the module object with your data object.

在第二部法典中,你基本上推翻了出口目标。 因此,即使你的法典奏效,我猜测所有其他出口都将被销毁(改写)。 因此,也许不存在某种保护机制以避免出现这种情况。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签