It works, I’ve tested it. What I want to know is – is this good practice. Obviously I could have just used an associative array and called it. But I wanted to encapuslate it into an object with state.
var View_message = function(div)
{ this.messages =
{
empty: 'Please complete all fields',
empty_bm: 'Please enter both a title and url',
name: 'Only letters or dashes for the name field',
email: 'Please enter a valid email',
same: 'Please make emails equal',
taken: 'Sorry that email is taken',
pass: 'Please enter a valid password, 6-40 characters',
validate: 'Please contact <a class="d" href="mailto:support@archemarks.com">support</a> to reset your password',
url: 'Pleae enter a valid url'
};
this.div = div;
};
View_message.prototype.display = function(type)
{
document.getElementById(this.div).innerHTML=this.messages[type];
};
obj_view = new View_message('test_id')
obj_view.display('empty');
You will be creating a copy of
this.messagesfor each instance ofView_message. To prevent that, you could store the messages in the prototype.Also, you are doing a lookup of the div every time you display a message. I’d store a reference to the div in the constructor instead.