On Oct 16, 2013, at 01:50, yougen zhuang <[email protected]> wrote:

> I also wondered how node re-point the "exports" reference, could you explain 
> it for me? Does not something like var module = {exports: {}}, if change 
> exports, why module.exports can't update?

Nothing is being "re-pointed". Everything is behaving as it normally does in 
JavaScript.

Before running a module, node creates a variable, equivalent to what you 
suggested:

var module = {exports: {}};

When a module is run, it's wrapped in its own scope, analogous to the following 
IIFE:

!function(module, exports) {
  /* your module code will be running here */
}(module, module.exports);

So within that function, you have your own *local* variables called "module" 
and "exports". You can redefine them if you want to use them for different 
purposes; it will have no bearing on the "module" variable node created outside 
the function scope; those local variables will cease to exist after the module 
has run. If you want to affect the "module.exports" property, you have to set 
"module.exports". "exports" is just a convenience so that you don't have to 
keep typing "module.exports" all the time, except that that can't work if you 
want to change what "module.exports" points to.

-- 
-- 
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.

Reply via email to