What is the connection between the outer var Message and the inner var Message?
The Call
new Message ( element ).display( server_response_text.slice( 6 ) );
Object
var Message = ( function ()
{
var messages =
{
name: 'Please enter a valid name',
email: 'Please enter a valid email',
email_s: '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',
same: 'Please make emails equal',
taken: 'Sorry, that email is taken',
validate: 'Please contact <a class="d" href="mailto:here@host.com">support</a> to reset your password',
};
var Message = function (element)
{
this.element = element;
};
Message.prototype.display = function( type )
{
this.element.innerHTML = messages[ type ];
new Effects().fade( this.element, 'down', 4000 );
};
return Message;
} () );
My entire library is using the module pattern like this
var NS = ( function ( window, undefined )
{
/*
all code here including Message
*/
} )( window );
There is no relationship1 to the variable names at all.
It is just the name used inside. It might as well be
fuubar:It makes sense that the outer name is
Message, because that is how it is “known” to others. 1The inner name was likely chosen for consistency, but otherwise does not affect semantics.Constructors are just function-objects [designed to be] invoked with the new operator. Variables can name objects. In this case note that it is the function-object that is returned. Thus the function-object is known by the name
Message(e.g. “is assigned to Message”) outside. However, a variable is not an object.Thus
new X()evaluates the expressionX, which must evaluate to a function-object, and then uses it as a constructor. (A constructor is just a normal function that expects to be used withnew: it may perform initialization and establish a [[prototype]]). The nameXis not important except insofar as it must evaluate to a function-object. Consider this equivalent form to show:new (X)(). The expression(X)is evaluated first.So, let’s break down the code:
Happy coding.