在 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 the
other, then they're no longer the same reference.
> BTW, if I modify to module.exports.Rect = Rect, then module.exports ===
> exports
>
> function Rect(w,h) {
> this.w = w;
> this.h = h;
> };
>
> Rect.prototype.area = function(){
> return this.w*this.h;
> };
>
> console.log(module.exports === exports);//true
> module.exports = Rect;
> console.log(module.exports);
> console.log(exports);
> console.log(module.exports === exports);//false
Yes, that's all correct. You're describing how JavaScript works.
Perhaps this helps:
When your module starts, you're given two variables: module (a reference to the
module object) and exports (a reference to module.exports).
It is module.exports that you need to fill with whatever it is you want your
module to export.
If you want to export multiple things, you can add properties to it:
module.exports.foo = function () { … }
module.exports.bar = function () { … }
In JavaScript we often assign shortcut variables for deep paths that we access
often, for example:
var exports = module.exports;
exports.foo = function () { … }
exports.bar = function () { … }
And it just so happens that because this is so common, the module system has
already done "var exports = module.exports" for you so you don't need to do it
manually.
But if you want to export only a single thing and you want it available on
module.exports itself then you cannot do that by assigning to the exports
variable:
exports = function () { … }
This would only change what your local exports variable points to and has done
nothing to module.exports.
If you want to change what module.exports points to you have to assign it
directly:
module.exports = function() { … }
And if you want to be complicated and do both, then you can do what was
suggested earlier in the thread so that the local exports variable is also
updated:
module.exports = exports = function() { … }
On Oct 17, 2013, at 20:57, yougen zhuang <[email protected]> wrote:
> 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();
Yes, correct. As you're already aware, if you want to reassign the
module.exports property, you need to assign to module.exports, not exports.
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.