Creating an object with privacy and prototyping(templating) similar to C++ class-private
<p>**EDIT - this was not stated explicitly in the answers...but you can not have private members that are "templated (i.e. not copied for each instance)" with out a self executing function.  You will pay with processor time and code complexity for privacy</p>

<p>Like a C++ private class member.</p>

<p>I've been trying to determine what the defacto "hack" is for creating classes  Seems like there are over 10 ways people are defining classes.</p>

<p>The type of class I'm looking for provides privacy and modularization and a templating mechanism.  Javascript has prototype to create this templating mechanism.  However the solution I am currently using below does not have private members.</p>

<p>If there isn't I have to ask the question what is the cost if I move all my javascript  to this class type...where each is a self executing function?  Now when my code loads it all has to be run before it can be used.</p>

<p>This cost is a trade-off for better programming practice.  Now I have proper classes with privacy.</p>

<p>Is the cost worth it?  Is the extra run time worth the privacy?</p>

<p>Here are some similar questions:</p>

<ul>
<li><a href="http://stackoverflow.com/questions/8130245/how-do-i-group-similar-methods-and-variables-together-in-an-object-oriented-appr">An object oriented approach - 3 ways</a></li>
<li><a href="http://stackoverflow.com/questions/8043611/how-do-i-consolidate-methods-into-objectsexcellent-in-progress">Creating a javascript object with prototyping (no privacy)</a></li>
<li><a href="http://stackoverflow.com/questions/8045897/should-i-consolidate-similar-methods-into-javascript-objectsassociative-arrays">Menu Object lack privacy - How to add w/ out self-executing method?</a></li>
<li><a href="http://stackoverflow.com/questions/1114024/constructors-in-javascript-objects">Constructors in Javascript objects</a></li>
<li><a href="http://stackoverflow.com/questions/8618944/wrapping-a-javascript-class">Wrapping a javascript &quot;class&quot; w/ out self execution?</a></li>
<li><a href="http://www.phpied.com/3-ways-to-define-a-javascript-class/" rel="nofollow">http://www.phpied.com/3-ways-to-define-a-javascript-class/</a></li>
</ul>

<p>Current Method - lacks privacy...is spread out..ie. not enclosed in parantheses</p>

<pre><code>var Message = function( div ) 
{
    this.div = document.getElementById( div ); 
};

Message.prototype.messages = 
{ 
    name:       'Please enter a valid name',
    email:      'Please enter a valid email',
    pass:       'Please enter passoword, 6-40 characters',
    url:        'Pleae enter a valid url',
    title:      'Pleae enter a valid title',
    tweet:      'Please enter a valid tweet',
    empty:      'Please complete all fields',
    same:       'Please make emails equal',
    taken:      'Sorry, that email is taken',
    validate:   'Please contact &lt;a class="d" href="mailto:support@host.com"&gt;support&lt;/a&gt; to reset your password'
};

Message.prototype.display = function( type ) 
{
    Control.sendInner( this.div, this.messages[type] );
};
</code></pre>
