为什么我的函数应该返回数组时返回未定义?

时间:2020-09-22 16:59:14

标签: javascript class constructor

我正在为钢琴教师创建一种解决方案,以无纸化方式记录学生的所有作品。我遇到一个错误:每当我运行函数时,它都会返回未定义状态。这是我的代码:

MultipartRequest

错误:

class Student{ constructor(name){ this.name = name; } } class StudentContainer{ constructor(studentArr){ this.students = studentArr; } } StudentContainer.prototype.studentExists = function(nameToSearch){ for(var th in this.students){ if(this.students[th].name == nameToSearch){ return [true, parseInt(th)]; break; }else if(parseInt(th)+1 == this.students.length){ return [false, -1]; }else{ } } } StudentContainer.prototype.addStudent = function(studentToAdd){ this.students[this.students.length] = studentToAdd; } StudentContainer.prototype.addStudentConditional = function(studentToAdd){ var exists = this.studentExists(studentToAdd.name); console.log(exists); //undefined if(exists[0] == true){ return {"success": false, "index": exists[1]}; }else{ this.students[this.students.length] = studentToAdd; return {"success": true, "index": this.students.length-1}; } } var y = new StudentContainer([]); y.addStudentConditional(new Student("Sample")); 怎么了?有工作片段吗?

0 个答案:

没有答案
相关问题