**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
Like a C++ private class member.
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.
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.
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.
This cost is a trade-off for better programming practice. Now I have proper classes with privacy.
Is the cost worth it? Is the extra run time worth the privacy?
Here are some similar questions:
- How do I group similar methods and variables together in an object oriented approach?
- Creating a javascript object with prototyping (no privacy)
- https://stackoverflow.com/questions/8045897/should-i-consolidate-similar-methods-into-javascript-objectsassociative-arrays
- Constructors in JavaScript objects
- Wrapping a JavaScript "class" w/ out self execution?
- http://www.phpied.com/3-ways-to-define-a-javascript-class/
Current Method - lacks privacy...is spread out..ie. not enclosed in parantheses
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 <a class="d" href="mailto:support@host.com">support</a> to reset your password'
};
Message.prototype.display = function( type )
{
Control.sendInner( this.div, this.messages[type] );
};