javascript JavaScript库示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript JavaScript库示例相关的知识,希望对你有一定的参考价值。
var g = G$('John', 'Doe', 'en');
g.greet().setLang('es').greet(true);
$('#login').click(function(){
var loginGrtr = G$('John', 'Smith');
$('#logindiv').hide();
loginGrtr.setLang($('#lang').val()).HTMLGreeting('#greeting', true).log();
});
(function(global, $) {
var Greetr = function(firstname, lastName, language){
return new Greetr.init(firstname, lastName, language);
}
var supportedLanguages = ['en', 'es'];
var greetings = {
en: 'Hello',
es: 'Hola'
}
var formalGreetings = {
en: 'Greetings',
es: 'Solados'
}
Greetr.prototype = {
fullName: function(){
return this.firstName + ' ' + this.lastName;
},
validate: function(){
if (supportedLanguages.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 = this.formalGreeting();
}
else {
msg = this.greeting()
}
console.log(msg);
return this;
},
log: function(){
if (console) {
console.log(this.language + ': ' + this.fullName());
}
return this;
},
setLang: function(lang){
this.language = lang;
this.validate();
return this;
},
HTMLGreeting: function(selector, formal){
if (!$) {
throw "jQuery not loaded";
}
if (!selector) {
throw "Missing selector";
}
var msg;
if (formal) {
msg = this.formalGreeting();
}
else {
msg = this.greeting()
}
$(selector).html(msg);
return this;
}
};
Greetr.init = function(firstName, lastName, language){
var self = this;
self.firstName = firstName || '';
self.lastName = lastName || '';
self.language = language || 'en';
self.validate();
}
Greetr.init.prototype = Greetr.prototype;
global.Greetr = global.G$ = Greetr;
})(window, jQuery);
以上是关于javascript JavaScript库示例的主要内容,如果未能解决你的问题,请参考以下文章
用于甘特图的 JavaScript / jQuery 库
JavaScript-Tool-lhgDialog:动画示例
为啥要设计一个库来在 Javascript 中使用 const [] 而不是 const ?
ajax请求, 前后端, JavaScript与java代码示例
将 java 日期时间符号转换为 javascript 库日期时间符号
jQuery部分选择器示例