1.2.8.5.增加 jQuery 支援
// 原型物件中的this會指向物件本身
Greetr.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.fistname + '!';
},
formalGreeting: function() {
return formalGreetings[this.language] + this.fistname + '!';
},
//可鍊結的方法
greet: function(formal) {
var msg;
if (formal) {
msg = this.formalGreeting();
} else {
msg = this.greeting();
}
if (console) {
console.log(msg);
}
return this
},
log: function() {
if (console) {
console.log(logMessage[this.language]);
}
return this;
},
setLang: function(lang) {
this.language = lang;
this.validate();
},
//jQuery method
HTMLGreeting:function(selector, formal) {
if (!$) {
throw 'jQuery not loaded';
}
if (!selector) {
throw 'Missing jQuery selector';
}
var msg;
if (formal) {
msg = this.formalGreeting();
} else {
msg = this.greeting();
}
$(selector).html(msg);
return this;
},
};
Last updated
Was this helpful?