-1

我觉得奇怪的是,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])));
4

2 回答 2

1

我认为最好在原型上设置方法,但我不明白你为什么要这样更改构造函数,你可以使用Object.create(或非标准的__proto__

于 2013-05-16T20:15:35.650 回答
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()));
于 2013-05-16T20:51:40.067 回答