简体   繁体   English

Javascript匿名函数向其添加对象

[英]Javascript anonymous function adding object to it

Can you explain what going on or some link to understand below code? 您能解释下面的代码是怎么回事或一些链接来理解吗?

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;
}

Here type of app is function but where other object is assigned and then after return how to access these object 这里的应用程序类型是函数,但是在其中分配了其他对象,然后在返回后如何访问这些对象

A little explanation: 一点解释:

app is named function: app名为函数:

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

The mixin you see after are used to extend the properties on the original app object. 您之后看到的mixin用于扩展原始app对象上的属性。

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

Below, this code define some properties on app.request you can look at docs for more infos. 下面的代码在app.request上定义了一些属性,您可以查看文档以获取更多信息。

The Object.defineProperties() method defines new or modifies existing properties directly on an object, returning the object. Object.defineProperties()方法直接在对象上定义新属性或修改现有属性,并返回该对象。

In JS functions are objects so the can have their own properties (in this case request ). 在JS函数中,对象是对象,因此可以具有自己的属性(在这种情况下为request )。

// 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 }
  })

The code initialize app object: 代码初始化app对象:

app.init();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM