方法过载
上一节,我们在构造函数里利用this关键字创建blog对象的方法。但是根据这个创建的每个blog对象,均各有一份对象方法的副本!浪费!
方法应该为各个对象所共享。
共享方法
多亏了每个对象有个隐藏对象prototype(它以特性的形式存在),JavaScript的类才能成真。prototype对象用于设定隶属于类层的特性与方法,而非属于实例的!
所以,使用prototype对象,即可创建类拥有的方法。
不用再放在构造函数里,放在外部。
// Blog object constructor
function Blog(body, date) {
// Assign the properties
this.body = body;
this.date = date;
}
// Return a string representation of the blog entry
Blog.prototype.toString = function() {
return "[" + (this.date.getMonth() + 1) + "/" + this.date.getDate() + "/" +
this.date.getFullYear() + "] " + this.body;
};
// Return a formatted HTML representation of the blog entry
Blog.prototype.toHTML = function(highlight) {
// Use a gray background as a highlight, if specified
var blogHTML = "";
blogHTML += highlight ? "<p style='background-color:#EEEEEE'>" : "<p>";
// Generate the formatted blog HTML code
blogHTML += "<strong>" + (this.date.getMonth() + 1) + "/" + this.date.getDate() + "/" +
this.date.getFullYear() + "</strong><br />" + this.body + "</p>";
return blogHTML;
};
// See if the blog body contains a string of text
Blog.prototype.containsText = function(text) {
return (this.body.toLowerCase().indexOf(text.toLowerCase()) != -1);
};
共享属性
就像Java里的static 属性,每个实例都是共享这个属性的。事实上,我们也使用prototype创建属性即可。
blog.prototype.signature="Puzzler Ruby";
扩展标准对象
扩展对象的关键就是prototype对象,因为每一个javascript对象里都有它,所以扩展任何对象,只需要把特性和方法加入它的prototype对象里,借以建立类特性和类拥有的方法。
假如我们要扩展String对象,我们使用如下代码:
String.prototype.scramble=function(){
//return scrambled string
}
我们要使用这个scramble()方法,只需要在字符串变量处调用即可:
alert(this.signature.scramble());
同理,假如要使用Date对象,因为Date.toString()方法返回的字符串不尽如人意,所以我们加入新方法即可。
Date.prototype.shortFormat = function() {
return (this.getMonth() + 1) + "/" + this.getDate() + "/" + this.getFullYear();
}
/** this.date.shortFormat()调用*/
再看构造函数
function Blog(body, date, image) {
// Assign the properties
this.body = body;
this.date = date;
this.image = image;
}
Blog.prototype.toHTML = function(highlight) {
// Use a gray background as a highlight, if specified
var blogHTML = "";
blogHTML += highlight ? "<p style='background-color:#EEEEEE'>" : "<p>";
// Generate the formatted blog HTML code
if (this.image) {
blogHTML += "<strong>" + this.date.shortFormat() + "</strong><br /><table><tr><td><img src='" +
this.image + "'/></td><td style='vertical-align:top'>" + this.body + "</td></tr></table><em>" +
this.signature + "</em></p>";
}
else {
blogHTML += "<strong>" + this.date.shortFormat() + "</strong><br />" + this.body +
"<br /><em>" + this.signature + "</em></p>";
}
return blogHTML;
};
var blog = [ new Blog("Got the new cube I ordered. It's a real pearl.", new Date("08/14/2008")),
new Blog("Solved the new cube but of course, now I'm bored and shopping for a new one.", new Date("08/19/2008")),
new Blog("Managed to get a headache toiling over the new cube. Gotta nap.", new Date("08/16/2008")),
new Blog("Found a 7x7x7 cube for sale online. Yikes! That one could be a beast.", new Date("08/21/2008")),
new Blog("Met up with some fellow cubers to discuss the prospect of a 7x7x7 cube. Mixed feelings.", new Date("08/29/2008")),
new Blog("Went ahead and ordered the scary 7x7x7 cube. Starting a mental exercise regimen to prepare.", new Date("09/01/2008")),
new Blog("Attended a rally outside of a local toy store that stopped carrying cube puzzles. Power to the puzzlers!", new Date("09/03/2008")),
new Blog("Got the new 7x7x7 cube. Could be my last blog post for a while...", new Date("09/05/2008")),
new Blog("Wow, it took me a couple of weeks but the new cube is finally solved!", new Date("09/19/2008"), "cube777.png") ];
由上述3段可知,构造函数传参可以不传全部,没有的部分为空。