向对象方法传递字符串数组(Javascript)

时间:2018-10-22 20:24:04

标签: javascript arrays methods

目标是将一个字符串数组(替代教师姓名)传递给“ pickSubstituteTeacher”方法,并返回一位随机教师。我无法弄清楚如何将字符串数组发送给object方法并返回随机值。

class School {
  constructor(name, level, numberOfStudents) {
    this._name = name;
    this._level = level;
    this._numberOfStudents = numberOfStudents;
  }

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

const school1 = new School('school1', 'two', 233);


let randomTeacher = School.pickSubstituteTeacher['teacher1','teacher2','teacher3'];

console.log(randomTeacher);

2 个答案:

答案 0 :(得分:1)

您必须在数组周围加上括号,以表明该数组是该方法的参数。

let randomTeacher = School.pickSubstituteTeacher(['teacher1','teacher2','teacher3']);

答案 1 :(得分:1)

你太近了...

继续:let randomTeacher = School.pickSubstituteTeacher(['teacher1', 'teacher2', 'teacher3']);