我觉得奇怪的是,instructors_students 函数与任何课程都没有关联,也不是讲师课程的方法。我想知道如果可能的话我会怎么做。代码按原样工作,但它是否真正面向对象以这种方式完成。社区的意见是什么?
function Person(person){
this.title = person.title;
this.firstname = person.firstname;
this.lastname = person.lastname;
}
Person.prototype.fullName = function(){
return this.title + " " + this.firstname + " " + this.lastname;
}
function Instructor(instructor){
this.id = instructor.id;
Person.call(this, instructor);
}
Instructor.prototype = new Person({});
Instructor.prototype.constructor = Instructor;
function Student(student){
this.id = student.id;
Person.call(this, student);
}
Student.prototype = new Person({});
Student.prototype.constructor = Student;
function Tutorial(tutorial){
this.id = tutorial.id;
this.instructor = tutorial.instructor;
this.day = tutorial.day;
this.begin = tutorial.begin;
this.finish = tutorial.finish;
}
function Register(register){
this.id = register.id;
this.year = register.year;
this.week = register.week;
this.tutorial = register.tutorial;
this.students = register.students;
}
function instructors_students(instructor){
students_taught = new Array();
registers.forEach(function(register){
if (register.tutorial.instructor == instructor){
register.students.forEach(function(student){
if (students_taught.indexOf(student) == -1){
students_taught.push(student);
}
});
}
});
return students_taught;
}
function list_students(students){
student_list = '';
students.forEach(function(student){
student_list += student.fullName() + ', ';
});
student_list.substring(0, student_list.length - 3);
return student_list;
}
function display(text){
var body = document.getElementsByTagName('body')[0];
var div = document.createElement('div');
var text = document.createTextNode(text);
div.appendChild(text);
body.appendChild(div);
}
// ** Load Data Here **
display('Instructor 1s Students: ' + list_students(instructors_students(instructors[1])));