2017-07-20 13 views
1

私は3つのコンストラクタを持っています。学校、教師、学生。 これまでのところ私のコードのすべては大丈夫だと感じましたが、私はteacher.prototype内で2つの関数を返すようには思えません。私はJsのに新しいですし、これはプロトタイプを継承しているクラスのコンストラクタで私のコンストラクタに私のプロトタイプを返すことはできません。

//create a constructor for a school that has all teachers and students 
function school(principal,teacher,student,classroom,detention){ 
    this.principal = principal, 
    this.teacher = teacher, 
    this.student = student; 
    this.classroom = [], 
    this.detention = [] 
} 
//create a constructor for teachers and students 
//link them all together 
function teacher(admin,name){ 
    this.admin = admin 
    this.name = name 
    admin = true 
//inherit some of the properties of school 

} 
function student(fname,lname,year,average){ 
    this.fname = fname, 
    this.lname = lname, 
    this.year = year, 
    this.average = average 
} 
teacher.prototype = Object.create(school.prototype); 
//teacher can send students to class 
teacher.prototype.learn = function(student){ 
    this.classroom.unshift(student) 
} 
//be able to move students to detention 
teacher.prototype.punish = function(student){ 
    this.detention.unshift(student) 
} 


student.prototype = Object.create(teacher.prototype) 
student.prototype.fullDetails = function(){ 
    return this.fname + ' ' + this.lname + " is in " + this.year + 'th' + ' grade and his average is ' + this.average; 
} 


var mr_feeney = new teacher(true,"Mr.Feeney") 
var corey = new student("corey","mathews",10,88) 
var shaun = new student("shaun","hunter",10,43) 
var top = new student("topanga","lawrence",10,43) 

shaun.learn(); 
+1

学校をどのように初期化していますか? – karthick

答えて

1

応答しない理由について学ぶことを試みている、あなたが継承しているかのコンストラクタを呼び出す必要があり、中に現在のオブジェクトのコンテキスト

あなたの学生のコンストラクタで、あなたは教師のコンストラクタを呼び出し、は先生からを継承しているすべてのメンバ変数を初期化し、この

function student(fname,lname,year,average){ 
    //initialize all the member variables on this object that are created in the teacher constructor by calling teacher.call(this) 
    teacher.call(this); 

    this.fname = fname, 
    this.lname = lname, 
    this.year = year, 
    this.average = average 
} 

を行う必要があります。

これは、あなたのクラス名

function student() 

ために大文字を使用し、慣習に固執する、またschool

function teacher(admin,name){ 
    school.call(this); 
    this.admin = admin 
    this.name = name 
    admin = true 
} 

teacher.prototype = Object.create(school.prototype); 

から継承するteacherと同じである

function Student() 

すべてでなければなりませんこれは、あなたは他のアーキテクチャを持っていると言われています奇妙なことが起こっている - 生徒は本当に教師と同じ方法をすべて継承するべきなのだろうか?教師は実際に学校と同じプロパティ/メソッドをすべて継承する必要がありますか?生徒のコンストラクタから教師のコンストラクタを呼び出すとき、管理者と名前のデフォルトの引数は何ですか?

+0

ご協力いただきありがとうございます。私はクラスでオブジェクト指向のjsを始めました。これは教師が学生の教室を変えたり、学生に拘留したりすることを任せている課題の1つでした。 – dutchkillsg

関連する問題