PapaSmurf
PapaSmurf

Reputation: 1035

node.js about exports

I've been trying to get my head around node.js so I've been taking apart the chat demo they made here http://chat.nodejs.org/

I'm getting round to understanding it apart from this line var fu = exports;. Could anyone help me out?

Upvotes: 2

Views: 206

Answers (1)

Raynos
Raynos

Reputation: 169531

exports is a special local variable in node.

It's basically a variable you can add properties to that will be exported when another module require's your module.

So var fu = exports; is saying alias exports to fu. This means you can add properties to fu and they will exported by default.

So there are two similar patterns

var MyModule = exports;

MyModule.x = ...;

or

var MyModule = { x: ... };

module.exports = MyModule

The important thing of both cases is that exports.x is set so that when you require MyModule you can use require("MyModule").x

Upvotes: 3

Related Questions