In the message object below, if I call it like this:
var message_object = new Message( response_element );
message_object.display( 'empty' );
is the messages array created for each call to new Message()…if so how can I ensure there is only one messages array, as that is all I need?
var Message = function( element )
{
var messages =
{
name: 'Please enter a valid name',
email: 'Please enter a valid email',
pass: 'Please enter passoword, 6-40 characters',
url: 'Please enter a valid url',
title: 'Please enter a valid title',
tweet: 'Please enter a valid tweet',
empty: 'Please complete all fields',
email_s: 'Please enter a valid email.',
same: 'Please make emails equal',
taken: 'Sorry, that email is taken',
validate: 'Please contact <a class="d" href="mailto:chris@domain.com">support</a> to reset your password',
}
this.display = function( type )
{
element.innerHTML = messages[ type ];
new Effects().fade( element, 'down', 4000 );
}
};
Yes, that entire function would be executed each time, and the messages object AND the function would be redefined each time.
What you probably want to do is wrap it in a closure, and make use of Javascript’s prototypical inheritance
This keeps the
messagesobject private to the ‘class’.