日期:2014-05-16 浏览次数:20469 次
//'Lencture'类的构造函数
//用名称(name)和教师(teacher)作为参数
function Lecture(name,teacher){
this.name = name;
this.teacher = teacher;
}
//Lecture类的一个方法(method),用于生成
//一条显示Lecture信息的字符串
Lecture.prototype.display = function(){
return this.teacher + ' is teaching ' + this.name + '!';
};
//Schedule类的构造函数,以课程的数组作为参数
function Schedule(lectures){
this.lectures = lectures;
}
//构造一条字符串,表示课程的安排表
Schedule.prototype.display = function(){
var str = '';
//遍历每项课程,建立包含他们信息的字符串
for(var i = 0; i < this.lectures.length; i++){
str += this.lectures[i].display() + '\n';
}
return str;
};
var mySchedule = new Schedule([
new Lecture("Gym","Mr.Smith"),
new Lecture("Math","Mrs.Jones"),
new Lecture("English","TBD")
]);
//调用mySchedule.display()
alert(mySchedule.display());

//调用mySchedule.display alert(mySchedule.display);
