数据+行为=对象
对象在一个存储容器内链接变量与函数。
对象需要构造函数,并且用关键字new创建对象:
function Blog(body, date) {
// Assign the properties
this.body = body;
this.date = date;
}
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")) ];
使用方法:(因为有了构造函数,所以我们可以直观的看出它有哪些成员变量)
// Generate the formatted blog HTML code
blogText += "<strong>" + (blog[i].date.getMonth() + 1) + "/" +
blog[i].date.getDate() + "/" +
blog[i].date.getFullYear() + "</strong><br />" +
blog[i].body + "</p>";
Date对象
Date提供了以下的方法:
getMonth()
:月份数,由0-11表示
getDate
:一个月的天数,由1-31表示
getFullYear()
:完整的四位整数年份
var now = new Date()
var now=new Date("08/14/2008")
计算时间:
function getDaysBetween(date1,date2){
var days=(date2-date1)/(1000*60*60*24);
return Math.round(days);
}
由代码可知,date是以毫秒计数的。
对象转换为文本
如果直接使用Date对象默认的toString(),输出是这样的:The Aug 14 2008 00:00:00 GMT-0500(CDT)
所以,我们要转化为方便我们理解的文本格式:
blogText += "<strong>" + (blog[i].date.getMonth() + 1) + "/" +
blog[i].date.getDate() + "/" +
blog[i].date.getFullYear() + "</strong><br />" +
blog[i].body + "</p>";
Date把排序变简单
数组Array有个方法叫sort()
,当然如果只靠数组的sort还不够,我们需要一个比较函数决定我们的排序行为。
function compare(x,y){
return x-y;
}
调用sort()方法时,把你自定义的compare()函数注射到数组排序的等式里:
nums.sort(compare);
利用函数字面量,使得排序变得简单:
blog.sort(function(blog1,blog2){
return blog2.date-blog1.date;
});
数组Array以及String
字符串是一个可以搜索的对象,它有以下方法:
length
属性:字符串长度
indexOf()
:查找子串起始位置
charAt()
:查找字符位置
toLowerCase()
:转换为小写
toUpperCase()
:转换为大写
现在我们试着来搜索匹配文本。
搜索字符串内部:indexOf()
for (var i = 0; i < blog.length; i++) {
// See if the blog entry contains the search text
if (blog[i].body.toLowerCase().indexOf(searchText.toLowerCase()) != -1) {
alert("[" + (blog[i].date.getMonth() + 1) + "/" + blog[i].date.getDate() + "/" +
blog[i].date.getFullYear() + "] " + blog[i].body);
break;
}
}
Math对象
Math对象包括了如下的方法和属性:
PI
属性:π值-3.14
round()
:四舍五入
floor()
:向下取整
ceil()
:向上取整
random()
:0-1之间的随机数
产生1-6的随机数:
var oneToSix=Math.floor(Math.random()*6)+1;
函数转变为方法
在构造函数里,写上this.方法名即可,如下:
function Blog(body, date) {
// Assign the properties
this.body = body;
this.date = date;
// Return a string representation of the blog entry
this.toString = function() {
return "[" + (this.date.getMonth() + 1) + "/" + this.date.getDate() + "/" +
this.date.getFullYear() + "] " + this.body;
};
// Return a formatted HTML representation of the blog entry
this.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
this.containsText = function(text) {
return (this.body.toLowerCase().indexOf(text.toLowerCase()) != -1);
};
}