gpt4 book ai didi

javascript - 将字符串数组传递给对象方法(Javascript)

转载 作者:行者123 更新时间:2023-12-01 01:34:39 26 4
gpt4 key购买 nike

目标是将一组字符串(替代教师姓名)传递给“pickSubstituteTeacher”方法并返回一名随机教师。我不知道如何将字符串数组发送到对象方法并返回随机值。

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);

最佳答案

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

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

关于javascript - 将字符串数组传递给对象方法(Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52937251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com