expressjs 源码 为什么用mixin拓展app, 而不是像下面用Object.create一样拓展req一样拓展app

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

代码Github页面地址点击这里

阅读 1.7k
1 个回答

mixin不会改变目标对象的原型,会在原有对象基础上增加(或者说混入)新的属性,而Object.create是创建一个指定原型和若干属性的对象。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题