javascript objects confusion

Possible Duplicate:
What is the purpose of NodeJS module.exports and how do you use it?

I have the following code:

var express = require('express');
var app = module.exports= express();
require('./config/environment.js')(app, express, __dirname);
require('./routes/default.js')(app, __dirname);


module.exports = function (app, express, dirname) {
....
};

module.exports = function (app, dirname) {
....
};

what happened in this code. Second string says, that module.exports and app are the same object, right?

but in function(...) parts app pass as parameter and that code likes on "to object 'module' add method 'exports' and do it 2 times" I want to add some functions, which want to use inside each function (...), but can't because don't understand what happens in that constructions. Thanks


Why are you assigning module.exports three times? In your code module.exports will first become equal to what ever is returned by calling express. Then module.exports will become equal to your function (NOT what it returns) and will take 3 arguments. Then module.exports will be equal to your final function (again NOT what it returns) taking 2 arguments. Therefore by the end of your code module.exports will be equal to that final function. So I don't see what the need is for the first two assignments. App will be equal to module.exports at the end because app is pointing to module.exports the whole time. It doesn't matter that you want app to be passed as an argument to it because no where in the code above do you actually pass app into the function, after assigning the functions to module.exports. All you have done here is name a parameter "app".

I think you have either missed code out here or got very confused by other languages you may have used in the past.

Lookup Douglas Crockford if the language isn't clear to you.

I hope that helps.

链接地址: http://www.djcxy.com/p/96624.html

上一篇: 在节点中导出属性?

下一篇: JavaScript对象混淆