-1

私は、instructors_students 関数がどのクラスにも関連付けられておらず、instructor クラスのメソッドではないことを奇妙に感じています。可能であれば、これを行う方法を知りたいです。コードはそのままで機能しますが、この方法で本当にオブジェクト指向になっているのでしょうか。コミュニティの意見は?

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 に答える