Search code examples
javascriptnode.js-connect

What JS design pattern is this?


I'm looking at the source code of connect js library and they do something interesting. They merge all of the methods from proto into the app object. Does this design pattern have a name?

function createServer() {
  function app(req, res, next){ app.handle(req, res, next); }
  utils.merge(app, proto);
  utils.merge(app, EventEmitter.prototype);
  app.route = '/';
  app.stack = [];
  for (var i = 0; i < arguments.length; ++i) {
    app.use(arguments[i]);
  }
  return app;
};

Solution

  • I think the closest thing to call it would be a mixin. That is, you take an existing definition of an object (in this case a function) and then copy properties from another object onto it.

    You can tell from reading the source that the method simply copies properties from the source object to the target object.