87,910
社区成员
发帖
与我相关
我的任务
分享
//类Lecture的构造器
//使用两个字符串参数,name和teacher
function Lecture( name, teacher ) {
//把它们作为对象的本地属性保存
this.name = name;
this.teacher = teacher;
}
//类Lecture的方法,生成一个显示该课程信息的字符串
Lecture.prototype.display = function(){
return this.teacher + " is teaching " + this.name;
};
//类Schedule的构造器
//使用一个lectures类型的数组作为参数
function Schedule( lectures ) {
this.lectures = lectures;
}
//类Schedule的方法,用来构造一个描述该课程表的字符串
Schedule.prototype.display = function(){
var str = "";
//遍历每门课程,累加构成信息字符串
for ( var i = 0; i < this.lectures.length; i++ )
str += this.lectures[i].display() + " ";
return str;
};
var mySchedule = new Schedule([
//创建一个课程对象的数组,
//作为传给课表(原文此处为Lecture,疑为笔误)对象的唯一参数
new Lecture( "Gym", "Mr. Smith" ),
new Lecture( "Math", "Mrs. Jones" ),
new Lecture( "English", "TBD" )
]);
// 弹出对话框显示课表的信息
alert( mySchedule.display() );