鍍金池/ 問答/HTML/ expressjs 源碼 為什么用mixin拓展app, 而不是像下面用Obje

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頁面地址點擊這里

回答
編輯回答
巷尾

mixin不會改變目標對象的原型,會在原有對象基礎上增加(或者說混入)新的屬性,而Object.create是創(chuàng)建一個指定原型和若干屬性的對象。

2017年9月28日 11:04