`exports` is simply a variable containing a reference to `module.exports`. 
 `module.exports` is an empty object to start, so you can start assigning 
stuff to its properties right away.

So when you have `var app = exports`, you're using `app` as a reference to 
the object assigned to `module.exports`, so any changes you make to that 
will be exported.

When you have `var app = {}` and assign `exports = app`, you overwrite the 
reference from `exports` to `module.exports` so `module.exports` never gets 
modified.  If you instead had `module.exports = app` then you would 
successfully export whatever is in `app`.

In general, use `module.exports` and avoid `exports` altogether.

On Sunday, July 29, 2012 10:39:29 PM UTC-4, 软刀 wrote:
>
>  # a.js :
>
> #! /usr/bin/env node
> // coding: utf-8
> // author: ruandao(ljy080...@gmail.com)
>
> var app = exports;
> app.yaha = 'jjj';
> app.yahajk = 'eklj';
> app.echo = function(){
>   console.log(this.yaha);
> };
>
>
> and I run in node:
>
> > yan@cpu:~$ node
> > a=require('./a');
> { yaha: 'jjj',
>   yahajk: 'eklj',
>   echo: [Function] }
>
>
>
> but why , I never assignment *app* to *exports *, it should be undefined 
> object!
> *
> *
>
> =======================================================================================================
>
> and again, when I wrote this:
>  # a.js :
>
> #! /usr/bin/env node
> // coding: utf-8
> // author: ruandao(ljy080...@gmail.com)
>
> var app = {};
> app.yaha = 'jjj';
> app.yahajk = 'eklj';
> app.echo = function(){
>   console.log(this.yaha);
> };
> exports = app;
>
>
> in node :
>
> > yan@cpu:~$ node
> > a=require('./a');
> {}
> > var c = {};
> undefined
> > c.yaha = 'jjj';
> 'jjj'
> > c
> { yaha: 'jjj' }
> > 
>
> why app's peoperties: *yaha, yahajk *and it's method *echo *lose
>
>
> thanks very much!
>
>
>

-- 
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to