title: How does JavaScript .prototype work?
body: <p>I'm not that into dynamic programming languages but I've written my fair share of JavaScript code. I never really got my head around this prototype-based programming, does any one know how this works?</p>
  <pre><code>var obj = new Object();
  obj.prototype.test = function() { alert('Hello?'); };
  var obj2 = new obj();
  obj2.test();
  </code></pre>
  <p>I remember a lot discussion I had with people a while back (I'm not exactly sure what I'm doing) but as I understand it, there's no concept of a class. It's just an object, and instances of those objects are clones of the original, right?</p>
  <p>But what is the exact purpose of this &quot;.prototype&quot; property in JavaScript? How does it relate to instantiating objects?</p>
  <h3>Update: correct way</h3>
  <pre><code>var obj = new Object(); // not a functional object
  obj.prototype.test = function() { alert('Hello?'); }; // this is wrong!
  
  function MyObject() {} // a first class functional object
  MyObject.prototype.test = function() { alert('OK'); } // OK
  </code></pre>
  <p>Also these <a href="http://ejohn.org/apps/learn/#64" rel="noreferrer">slides</a> really helped a lot.</p>