9-73 Requirements

  • Greeter
  • input : firstname lastname and optional language
  • output : formal and informal greeting sentence
  • language support English and Spanish
  • reusable
  • just G$() to call (no ‘new’)
  • support jQuery(give selector, add greeting sentence to the selected element)
Greeter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

;(function (global, $){
// trick , avoid the problem caused by other lib that doesn't end with semicolon

// trick to use our lib without new
// function declare, it is not actually run
// so we can set Greeter.init later
var Greeter = function(firstName,lastName,language){
return new Greeter.init(firsname,lastname,language);
}

// setup prototype of greeter
Greeter.prototype = {
fullName : function(){
return this.firstName + ' ' + this.lastName;
},

validate : function (){
if (supportedLangs.indexOf(this.language) === -1){
throw 'Invalid language';
}
},

greeting : function(){
return greetings[this.language] + ' ' + this.firstName + '!';
}

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

greet : function (formal){
var msg ;
if(formal){
msg = formalgreeting();
}
else{
msg = greeting();
}

if(console){
console.log(msg);
}

return this;
},

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

setLang : function(lang){
this.language = lang;

this.validate();

return this;
},

// support jQuery
HTMLGreeting : function(selector, formal){
if(!$){
throw 'jQuery isnot loaded';
}
if(!selector){
throw 'Missing jQuery selector';
}
var msg;
else{
msg = this.greeting()
$(selector).html = msg;

return this;
}
}

};

// available in init function because of closeure
// but these var can't be access from global
var supportedLangs = ['en','tw'];

var greetings = {
en : 'Hello',
tw : '哈囉'
};

var formalgreetings = {
en : 'Greetings',
tw : '你好'
};

var logMessages = {
en : 'Logged in',
tw : '登入'
}

// real constructor
Greeter.init = function (firstName,lastName,language){
// safe this
// point to the object created by new ()init
var self = this;

// setting default value
var self.firstName = firstName || 'DefaultFirstname';
var self.lastName = lastName || 'DefaultLastname';
var self.language = language || 'en';

self.validate();
}

//setup prototype chain of the object created by new init()
Greeter.init.prototype = Greeter.prototype;

//expose greeter to the world
global.Greeter = global.G$ = Greeter;

}(window,jQuery));