Creating functions for an object in javascript
<p>As far as I can tell, there are two main ways of creating functions for an object in javascript.  They are:</p>

<p>Method A, make it in the constructor:</p>

<pre><code>function MyObject() {
    this.myFunc1 = function() {
        ...
    }
    this.myFunc2 = function() {
        ...
    }
    ...
}
</code></pre>

<p>Method B, add it to the prototype:</p>

<pre><code>function MyObject() {
    ...
}

MyObject.prototype.myFunc1 = function() {
    ...
}

MyObject.prototype.myFunc2 = function() {
    ....
}
</code></pre>

<p>Obviously if you did:</p>

<pre><code>MyObject.myFunc3 = function() {
    ....
}
</code></pre>

<p>then <code>myFunc3</code> would become associated with MyObject itself, and not any new objects created with the <code>new</code> keyword.  For clarity, we'll call it method C, even though it doesn't work for creating new objects with the <code>new</code> keyword.</p>

<p>So, I would like to know what the differences between the two are.  As far as I can tell they have the same effect logically, even if what's happening on the machine is different.</p>

<p>If I were to guess I would say that the only real difference is when you're defining them in the constructor like in method A, it creates a whole new function object for each object that's created, and Method B only keeps one copy of it (in MyObject), that it refers to any time it's called.  if this is the case, why would you do it one way over the other.  Otherwise, what is the difference between method A and method B.</p>
