Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 145 additions & 5 deletions Greetr.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,167 @@
(function(Global, $){
var Greetr = function(
(function(Global, $)
{
var Greetr = function
(
firstName,
lastName,
language){
language)
{
return new Greetr.init(
firstName,
lastName,
language
);
}

// hidden within the scope of the IIFE and neveer directly accessible
var supportedLangs = ['en', 'es'];

Greetr.prototype = {};
// informal greetings
var greetings = {
en: 'Hello',
es: 'Hola'
};

// formal greetings
var formalGreetings = {
en: 'Greetings',
es: 'Saludos'
};

// logger messages
var logMessages = {
en:'Logged in',
es: 'Inició sesión'
};

// prototype holds methods (to save memory space)
Greetr.prototype = {

// 'this' refers to the calling object at execution time
fullName: function()
{
return this.fullName.firstName + ' ' + this.lastName;
},

validate: function()
{
// check that is a valid language
// references the externally inaccessible 'supportedLangs' within the closure
if (supportedLangs.indexOf(this.language) === -1)
{
throw "Invalid language";
}
},

// retrieve messages from object by referring to properties
greeting: function()
{
return greetings[this.language] + ' ' + this.firstName + '!';
},

formalGreeting: function()
{
return formalGreetings[this.language] + ', ' + this.fullName();
},

// chainable methods return their own containing object
greet: function(formal)
{

// if undefined or null it will be coerced to 'false'
if (formal)
{
msg = this.formalGreeting();
}
else
{
msg = this.greeting();
}
if (console)
{
console.log(msg);
}

// 'this' refers to the calling object at execution time
// makes the method chainable
return this;
},

log: function()
{
if (console)
{
console.log(logMessages[this.language] + ': ' + this.fullName());
}

// make chainable
return this;
},

setLang: function(lang)
{

// set the language
this.language = lang;

// validate
this.validate();

// make chainable
return this;
},

HTMLGreeting: function(
selector,
formal)
{
if(!$)
{
throw 'JQuery not loaded';
}

if (!selector)
{
throw 'Missing jQuery selector';
}

// determine the message
var msg;
if (formal)
{
msg = this.formalGreeting();
}

else
{
msg = this.greeting();
}

// inject the message in the chosen place in the DOM
$(selector).html(msg);

// make chainable
return this;
}
};

// the actual object is created here, allowing us to 'new' an object without calling 'new'
Greetr.init = function(
firstName,
lastName,
language){
language)
{

var self = this;
self.firstName = firstName || '';
self.lastName = lastName || '';
self.language = language || 'en';
}

// trick borrowed from jQuery so we don't have to use the 'new' keyword
Greetr.init.prototype = Greetr.prototype;

// attach our Greetr to the global object, and provide a shorthand '$G' for ease our poor fingers
Global.Greetr = global.G$ = Greetr;

}(window, JQuery));
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
var g = G$('John', 'Doe');
console.log(g);

g.greet().setLang('es').greet(true).log();
18 changes: 18 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<head>

</head>
<body>
<div id="logindiv">
<select id="lang">
<option value="en">English</option>
<option value="es">Spanish</option>
</select>
<input type="button" value="Login" id="login" />
</div>
<h1 id='greeting'></h1>
<script src="jquery-1.11.2.js"></script>
<script src="Greetr.js"></script>
<script src="app.js"></script>
</body>
</html>