javascript

来源:互联网 发布:跳跃网络服务器爆炸 编辑:程序博客网 时间:2024/04/29 07:42

以下代码是从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>

原创粉丝点击