title: How do I use a prototype in a JavaScript factory function?
body: <p>I've recently found an interest in the factory function pattern in JavaScript. Classes may be pretty clean, but I've found they've got their fair share of issues. (I'm not going to even talk about them because that isn't at all the purpose of this question.)</p>
  
  <p>There is one major aspect of factory functions I'm not clear on however.</p>
  
  <p>Correct me if I'm wrong, but in a JavaScript class my methods are placed on the resulting object's prototype so they are only ever created once. This means the constructor function used internally by the class won't be adding the methods as properties to every single new object, meaning conceptually that the methods belong to the class and not the object instance.</p>
  
  <pre class="lang-js prettyprint-override"><code>class Example {
    constructor(message) {
      this.message = message;
    }
    sayMessage() {
      console.log(this.message);
    }
  }
  
  let a = new Example("Hello!");
  a.sayMessage(); // Outputs "Hello!"
  
  console.log(Object.getOwnPropertyNames(a));
  // Outputs ["message"]
  
  Example.prototype.sayMessage = function() {
    console.log(this.message + " Modified!");
  };
  
  let b = new Example("Hello!");
  b.message = "Goodbye!";
  b.sayMessage(); // Outputs "Goodbye! Modified!"
  </code></pre>
  
  <p>In the class above, <code>sayMessage</code> is the same function for every instance of the class. As demonstrated, it can even be changed on the prototype later, updating all existing instances of the class. I can't say for sure whether I think this is good thing, but it certainly makes sense.</p>
  
  <p>However, in a factory function it seems that the object returned just has all needed methods attached to it as normal properties.</p>
  
  <pre class="lang-js prettyprint-override"><code>function Example(message) {
    let sayMessage = function() {
      console.log(this.message);
    };
  
    return {
      message: message,
      sayMessage: sayMessage
    };
  }
  
  let a = Example("Hello!");
  a.sayMessage(); // Outputs "Hello!"
  
  console.log(Object.getOwnPropertyNames(a));
  // Outputs ["message", "sayMessage"]
  
  // Modifying the prototype is pointless because
  // we haven't explicitly placed any methods there
  
  let b = Example("Hello!");
  b.message = "Goodbye!";
  b.sayMessage(); // Outputs "Goodbye!"
  </code></pre>
  
  <p>I'd like to talk about this a bit. So first of all, is avoiding the prototype part of the whole point of using a factory function in the first place? Why would we not create a function once and instead revert to duplicating it across every instance?</p>
  
  <p>I like the idea that I have more control over what I expose in the final object when I use a factory function because I can keep things private in the closure, but can I still use a prototype inheritance sort of model? Or perhaps an even better question: Would I want to? One of the main arguments I found against factory functions is that they are slower than classes in performance critical applications. Well isn't this here part of the reason? Creating a whole bunch of methods for every new object sounds like a huge waste. It sounds like the whole point of having a prototype!</p>
  
  <p>A lot of questions flying around here. Let me boil it all down. I'd like to hear what the rational would be for not using a prototype (or inversely for using one) and if there is a solution that includes the best of both worlds.</p>
  
  <p>Edit: When I first posted the question my second code example was creating <code>Example</code> instances with the <code>new</code> keyword, which was a typo.</p>