面向对象之Javascript浅析(上)
Javascript作为一门彻底的面向对象语言,随着其发展和已经其运用的扩大,其他语言的程序员开始注意它,并将许多良好的编程语言带到了Javascript,从而也催进了其发展。
然而不管在写法上还是在运行上,面向对象的Javascript代码和其他面向对象特性的语言都不一样。现在我们通过一个范例来学习下面向对象的Javascript。
// 用名称(name)和教师(teacher)作为参数
function Lecture(name,teacher){
//将参数保存为对象的局部属性(local Property)
this.name = name;
this.teacher = teacher;
}
//lecture类的一个方法(method),用户生成一条显示lecture信息的字符串
Lecturn.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 + ” “;
return str;
};
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments
还没有评论。
发表评论