JavaScript类方法及其属性。代码问题 - 如何将数组传递给属性?

时间:2017-11-13 21:50:22

标签: javascript arrays class methods properties

我在处理练习时遇到了麻烦(我对此很陌生),出于某种原因,我无法使用父方法+参数将数组传输到我的子类构造函数中。 aaaannnd我的代码可能还有很多其他问题,但我现在就试着调试自己。

class School {
  constructor(name, level, numberOfStudents) {
    this._name = name;
    this._level = level;              //['primary', 'middle', 'high'];
    this._numberOfStudents = numberOfStudents;
  }
  get name() {
    return this._name;
  }
  get level() {
    return this._level;
  }
  get numberOfStudents() {
    return this._numberOfStudents;
  }
  set numberOfStudents(newNumberOfStudents) {
    if (typeof newNumberOfStudents === number) {
      this._numberOfStudents = newNumberOfStudents
    } else {
      console.log(`The number of students must be numerical`);
      // might have to return something here
    }
  }

  quickFacts() {
    console.log(`${this.name} educates ${this.numberOfStudents} students, typically between the ages of ${this.level}.`);
  }

  static pickSubstituteTeacher(substituteTeachers) {
    let teacher = Math.floor(Math.random() * substituteTeachers.length);
    return substituteTeachers[teacher];
  }
}

class PrimarySchool extends School {
  constructor(name, numberOfStudents, pickupPolicy){
    super(name, 'primary', numberOfStudents);

    this._pickupPolicy = pickupPolicy;
  }
  get pickupPolicy() {
    return this._pickupPolicy;
  }
}

const lorraineHansbury = new PrimarySchool('Lorraine Hansbury', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.')

lorraineHansbury.quickFacts();

School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']);
console.log(School.pickSubstituteTeacher());

我很欣赏任何输入,并在格式化问题本身时对我在这里做错了的评论。

1 个答案:

答案 0 :(得分:0)

当您致电console.log(School.pickSubstituteTeacher());时,没有传递任何参数,因此它不会在此行let teacher = Math.floor(Math.random() * substituteTeachers.length);上工作,因为substituteTeachers未定义。

我已经解决了这个问题,如果您有任何其他问题,请告诉我

class School {
  constructor(name, level, numberOfStudents) {
    this._name = name;
    this._level = level;              //['primary', 'middle', 'high'];
    this._numberOfStudents = numberOfStudents;
  }
  get name() {
    return this._name;
  }
  get level() {
    return this._level;
  }
  get numberOfStudents() {
    return this._numberOfStudents;
  }
  set numberOfStudents(newNumberOfStudents) {
    if (typeof newNumberOfStudents === typeof Number) {
      this._numberOfStudents = newNumberOfStudents
    } else {
      console.log(`The number of students must be numerical`);
      // might have to return something here
    }
  }

  quickFacts() {
    console.log(`${this.name} educates ${this.numberOfStudents} students, typically between the ages of ${this.level}.`);
  }

  static pickSubstituteTeacher(substituteTeachers) {

   let teacher = Math.floor(Math.random() * substituteTeachers.length);
    return substituteTeachers[teacher];
  }
}

class PrimarySchool extends School {
  constructor(name, numberOfStudents, pickupPolicy){
    super(name, 'primary', numberOfStudents);

    this._pickupPolicy = pickupPolicy;
  }
  get pickupPolicy() {
    return this._pickupPolicy;
  }
}

const lorraineHansbury = new PrimarySchool('Lorraine Hansbury', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.')

lorraineHansbury.quickFacts();

console.log(School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']));
//console.log(School.pickSubstituteTeacher());