I am attempting to write code For the Bootcamp class (as shown in the code block) My goal is to write the code for the Bootcamp class so that if it the "students" parameter is not passed in, it is by default initialized to be an empty array from within the constructor parameter list itself. i took a stab at it with the function at the bottom of the code.
I am a bit confused regrading the proper syntax. Please See my Code Below
- Thank you in advance :)
class Bootcamp{
constructor(name,level,students){
this.name = name;
this.level = level;
this.students = students;
}
}
function student (students){
return [];
}
CodePudding user response:
You could use a default value if the param is not passed. In this way, your properties will be initialized with default values.
class Bootcamp{
constructor(name = '', level = 0, students = []){
this.name = name;
this.level = level;
this.students = students;
}
}
CodePudding user response:
Say you have a list of students or an empty array using the below edited codes
class Bootcamp{
constructor(name,level,students){
this.name = name;
this.level = level;
this.students = students || []; //if students undefined, set it to empty
//array
}
}
