閱讀connect.js源的時候,我不明白這行代碼:在connect.js源代碼中定義的應用程序在哪裏?
function app(req, res, next){ app.handle(req, res, next); }
的問題是:是app
定義在哪裏? app.handle(req, res, next);
https://github.com/senchalabs/connect/blob/master/lib/connect.js#L28
閱讀connect.js源的時候,我不明白這行代碼:在connect.js源代碼中定義的應用程序在哪裏?
function app(req, res, next){ app.handle(req, res, next); }
的問題是:是app
定義在哪裏? app.handle(req, res, next);
https://github.com/senchalabs/connect/blob/master/lib/connect.js#L28
app
在proto.js
定義:https://github.com/senchalabs/connect/blob/45fe02dde8b1f1da0dde0c593038753714f1d99b/lib/proto.js#L87
在JavaScript中,函數是做有屬性的對象。在下面的app
定義的線,它們合併使用utils-merge從proto.js
屬性:
merge(app, proto);
app
是與function app(....
第一行定義。
然而,app
由merge
膨脹-ing經由管線merge(app,proto)
使用util-merge的proto
模塊。
另請注意合併EventEmitter.prototype
,因爲這也很重要。
查看與connect.js
位於同一目錄中的proto.js
文件,您將看到導出的完整app
對象。
function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
}
function app(req, res, next){ app.handle(req, res, next); }
// ^^^ ^^^
它定義在那裏。 將handle
函數添加到app
以後(在proto.js文件中定義),使用merge(app, proto)
。
這裏的代碼做什麼簡單的例子:
function dog() {
dog.bark();
};
dog.bark = function() {
console.log('Woof!');
};
dog();
謝謝,我想用'proto.app'是在這條線更加清晰。 – why 2015-02-23 14:08:50