我觉得奇怪的是,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])));
答案 0 :(得分:1)
我认为在原型上设置方法会更好,但我不明白为什么你改变这样的构造函数,你可以使用Object.create
(或非标准的__proto__
)< / p>
答案 1 :(得分:0)
代码以它的方式工作,但它是否以这种方式真正以对象为导向。社区有什么意见?
是的,这将是更多的OOP。目前你的构造函数几乎什么都没做,而且只有一个共享方法。它可能是好的(甚至更清洁/更短)省略它们并且只是在普通数据上运行静态函数:
function fullName(person){
return person.title + " " + person.firstname + " " + person.lastname;
}
function instructors_students(instructor) {
var students_taught = [];
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) {
return students.map(fullName).join(', ');
}
function display(text) {
var body = document.getElementsByTagName('body')[0];
var div = document.createElement('div');
div.appendChild(document.createTextNode(text));
body.appendChild(div);
}
// ** Load Data Here **
display('Instructor 1s Students: ' + list_students(instructors_students(instructors[1])));
我想知道如果可能的话我会怎么做
当然有可能。该方法如下所示:
Instructor.prototype.getStudents = function() {
var instructor = this,
students_taught = [];
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;
}
及其调用:
display('Instructor 1s Students: '+list_students(instructors[1].getStudents()));