Hi, why do you need to do that? I don’t understand.
module.exports = exports = foo
On Tuesday, 15 October 2013 20:33:15 UTC+8, greelgorke wrote:
module.exports and exports are referencing the same object. if you do
> module.exports = foo, both point to different objects. if you want to
> ass
Kinda funny how hard it is to explain this.
I thought it was interesting so I gave it a crack as well:
http://www.noiregrets.com/blog/2013/10/19/e40baf63/NodeJS-module-exports-vs-exports
TL;DR: variables are not the values they point to.
Maybe that helps yougen or maybe I'm beating a dead horse.
I find this easier to explain in code:
var a = { b: {} }; // a = modules, a.b = modules.exports
var b = a.b; // b = exports
b.hello = 'world'; // set a key on "exports", works.
b = { goodbye_cruel: 'world' }; // set "exports" to a whole new object,
doesn't
console.log(a); // outputs: { b: { hello
On Friday, October 18, 2013 3:57:34 AM UTC+2, yougen zhuang wrote:
>
>
> exports = Rect;
>
>>
>>
Here is a helpful diagram that I often use to explain this:
http://i.imgur.com/bNG7JfZ.png
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing
在 2013年10月15日星期二UTC+8下午6时22分29秒,yougen zhuang写道:
> Before assign value to exports, module.exports === exports, but after assign
> value, they're not equal.
That's correct.
> In my view, they're point to the same reference.
Before you reassign them, that's correct. If you reassign one but not t
Another question:
//rect.js
function Rect(w,h) {
this.w = w;
this.h = h;
};
Rect.prototype.area = function(){
return this.w*this.h;
};
exports = Rect;
//main.js
var Rect = require(''rect.js");
//here will throw error===
var rect = new Rect();
在 2013年10月15日星期二UTC+8下午6时22分29秒,you
My question is not about how to keep reference intact, but why
module.exports and exports point to different object when only assign
module.exports
在 2013年10月16日星期三UTC+8下午4时29分59秒,greelgorke写道:
>
> there is no magic. at all. what whatever is assigned to module.exports is
> returned to requiring
there is no magic. at all. what whatever is assigned to module.exports is
returned to requiring code. the exports is initally set up to be a shortcut
reference to module.exports if you assign something to module.exports you
have to assign the same thing to exports if you want to keep this refere
Not exactly. If just setting "exports" would not work, for example, exports
= Rect, than in another file, assuming main.js, var Rect = require('Rect'),
var rect = new Rect() will fail.
在 2013年10月15日星期二UTC+8下午8时33分15秒,greelgorke写道:
>
> module.exports and exports are referencing the same object. i
I already read the doc, but the doc seems not explain the "magic" behind
the scene. If the doc can explain the detail just like @msk or link the
source code , it will be nicer.
在 2013年10月16日星期三UTC+8上午12时10分48秒,Ryan Graham写道:
>
> This behaviour is documented here:
> http://nodejs.org/api/modules
This behaviour is documented here:
http://nodejs.org/api/modules.html#modules_modules
If the explanation there is not clear, do you have any suggestions on how
to improve it? Is there another section in the docs that you looked for
this information first?
Suggestions welcome!
~Ryan
On Tue, Oc
module.exports and exports are referencing the same object. if you do
module.exports = foo, both point to different objects. if you want to
assign to module.exports and keep the shortcut exports correct do this
module.exports = exports = foo
simple reference handling, there is no magic behind.
12 matches
Mail list logo