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, $){
var Greeter = function(firstName,lastName,language){ return new Greeter.init(firsname,lastname,language); }
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; },
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; } }
};
var supportedLangs = ['en','tw'];
var greetings = { en : 'Hello', tw : '哈囉' };
var formalgreetings = { en : 'Greetings', tw : '你好' };
var logMessages = { en : 'Logged in', tw : '登入' }
Greeter.init = function (firstName,lastName,language){ var self = this;
var self.firstName = firstName || 'DefaultFirstname'; var self.lastName = lastName || 'DefaultLastname'; var self.language = language || 'en';
self.validate(); }
Greeter.init.prototype = Greeter.prototype;
global.Greeter = global.G$ = Greeter;
}(window,jQuery));
|