Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Below is a simple "class" for displaying messages. It stores the output html div and then displays messages to it as needed.

It works fine. For readability, and encapsulation purposes, I'd like the componenets of the "class" in a contianer of sorts or a javascript object? What is the simplest way to to do this.

Or...is there a simple way to do this?

Currently the only container is the comments I've surrounded the code with.

/**
 *      Module  :       Model
 *      Name    :       -Message
 *      Input   :       div via the constructor
                        message type via display
 *      Output  :       TextStream via display to Control.sendInner
 *      Notes   :       Symmetric, client holds message location so needs extra variable
 */

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',
    empty_bm:   'Please complete all fields',
    same:       'Please make emails equal',
    taken:      'Sorry, that email is taken',
    validate:   'Please contact <a class="d" href="mailto:[email protected]">support</a> to reset your password'
};

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


/**      Message End        */
share|improve this question
    
Please don't edit the question to make the existing answers meaningless. If you have new versions of the code post a new question. Reference this one (in it's original form) and explain that it's a new issue. meta.stackexchange.com/questions/94446/… meta.stackexchange.com/questions/64459/… –  palacsint Jan 3 '12 at 21:23

3 Answers 3

up vote 1 down vote accepted

For readability purposes, you could do something like this to create class definitions:

// utility function
UTIL = {
  createClass: function(o) {
    var F = o.constructor || function() { };
    for(var i in o) {
      F.prototype[i] = o[i];
    }
    return F;
  }
};

// class definition
Message = UTIL.createClass({

  constructor: function(div) { ... },

  messages: { ... },

  display: function() { ... }

});
share|improve this answer

You could wrap your whole file in a namespace, as described here.

Of all the possibilities, this is the one I like the most:

var YourNamespace = (function () {
   var Message = function (div) {
      ...
   }
   var aPrivateFunction = function () { ... }
   ...
   return {
      'Message': Message
   };
})();

This lets you define private functions, like aPrivateFunction, not visible outside YourNamespace. You can access Message as YourNamespace.Message.

share|improve this answer

The simplest way is to do something like this.

var My = {};

My.Message = function ( div ) 
{
    this.div = document.getElementById( div ); 
};

My.Message.prototype.messages = 
{ //... 
};

Here is another way that is unusual enough that linters (and peer-reviewers) will complain, but really it's harmless.

var My = new function(){

    this.Message = function ( div ) 
    {
        this.div = document.getElementById( div ); 
    };

    this.Message.prototype.messages = 
    { //... 
    };

}();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.