Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Of course it would create memory overhead. In the public case your function belongs to the <code>prototype</code> not to the instance, which means there is only one instance, unless you specifically give a specific object it's own instance of that function. In the private case the function belongs to the instance which means that you need to perform memory management.</p> <p>What I mean by this is the following:</p> <pre><code>var t1 = new Test(); t1.getPublicX = function () { return true; } var t2 = new Test(); t1.getPublicX(); // Returns true t2.getPublicX(); // Returns 15 </code></pre> <p>So you can end up in the same situation with public members as well. In general the answer to your question is: <strong><em>Yes there is a memory overhead when instantiating a large number of objects.</em></strong></p> <p>I should also add that the notion of <code>public</code> and <code>private</code> in <code>javascript</code> is not at all the same as in <code>C++</code>. In <code>C++</code> private encapsulates the member for access only from within the class, which in <code>javascript</code> you can still access the member from anywhere.</p> <p>And still after running a short test the overhead is in fact insignificant. The tab takes 40mb more (with google loaded) than without it as show in the screenshot below:</p> <p><img src="https://i.stack.imgur.com/k34TA.png" alt="enter image description here"></p> <p><a href="http://tinypic.com/r/9kv2q1/6" rel="nofollow noreferrer">Link to full size image.</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload