Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法理解此Express JS源代码片段_Javascript_Node.js_Express - Fatal编程技术网

Javascript 无法理解此Express JS源代码片段

Javascript 无法理解此Express JS源代码片段,javascript,node.js,express,Javascript,Node.js,Express,我试图理解Express JS源代码,这是导出Express的主要模块 module.exports = createApplication; function createApplication() { var app = function(req, res, next) { app.handle(req, res, next); }; mixin(app, EventEmitter.prototype, false); mixin(app, proto, fal

我试图理解Express JS源代码,这是导出Express的主要模块

module.exports = createApplication;


function createApplication() {
  var app = function(req, res, next) {
    app.handle(req, res, next);
  };

  mixin(app, EventEmitter.prototype, false);
  mixin(app, proto, false);

  // expose the prototype that will get set on requests
  app.request = Object.create(req, {
    app: { configurable: true, enumerable: true, writable: true, value: app }
  })

  // expose the prototype that will get set on responses
  app.response = Object.create(res, {
    app: { configurable: true, enumerable: true, writable: true, value: app }
  })

  app.init();
  return app;
}
我对这段代码感到困惑

  var app = function(req, res, next) {
    app.handle(req, res, next);
  };

变量
app
同时在函数内部分配和使用。这怎么行?其他任何地方都没有
应用程序的定义。找到真正的源。

函数被创建并分配给
app
变量。这是一个普通的函数表达式赋值

然后,两行
mixin()
app
函数添加方法。因此,在调用这些函数之后,它有类似于
app.handle()
app.init()
的功能

然后,添加另外两个属性
app.request
app.response

然后,调用app.init()

然后,稍后会调用
app
函数(当http请求到达时),当调用该函数时,它会调用
app.handle()
,该函数只是调用本身的属性函数。这是合法的。这类似于在更传统的对象中调用this.handle()

下面是一个让你最困惑的部分的演示:

var测试=函数(){
试验。剂量测定();
}
test.doSomething=函数(){
控制台日志(“doSomething”);
}

test();//导致调用test.doSomething()
Oh yes。现在有道理了。谢谢