以下代码是从pro javascript techniques书中拷贝出来的 <script language="JavaScript">
/**//*
* ex:Listing 1-1
*/
//Lecture类的构造函数
function Lecture( name, teacher ) {
this.name = name;
this.teacher = teacher;
}
//声明Lecture类的方法display
Lecture.prototype.display = function(){
return this.teacher + " is teaching " + this.name;
};
//Schedule类的构造函数
function Schedule( lectures ) {
this.lectures = lectures;
}
//声明Lecture类的方法display
Schedule.prototype.display = function(){
var str = "";
for ( var i = 0; i < this.lectures.length; i++ )
str += this.lectures[i].display() + "<br/>";
return str;
};
// 创建Schedule类的对象mySchedule
var mySchedule = new Schedule([
new Lecture( "Gym", "Mr. Smith" ),
new Lecture( "Math", "Mrs. Jones" ),
new Lecture( "English", "TBD" )
]
);
document.writeln( mySchedule.display() );
</script><script language="JavaScript"> /**//* * ex:Listing 1-1 */ //Lecture类的构造函数 function Lecture( name, teacher ) { this.name = name; this.teacher = teacher; } //声明Lecture类的方法display Lecture.prototype.display = function(){ return this.teacher + " is teaching " + this.name; }; //Schedule类的构造函数 function Schedule( lectures ) { this.lectures = lectures; } //声明Lecture类的方法display Schedule.prototype.display = function(){ var str = ""; for ( var i = 0; i < this.lectures.length; i++ ) str += this.lectures[i].display() + "
"; return str; }; // 创建Schedule类的对象mySchedule var mySchedule = new Schedule([ new Lecture( "Gym", "Mr. Smith" ), new Lecture( "Math", "Mrs. Jones" ), new Lecture( "English", "TBD" ) ] ); document.writeln( mySchedule.display() ); </script>