I'm trying to learn/understand how to recreate the behaviour of an object constructor (MultiStudentProto) in a class, where I'm only trying to make accessible the name of the students (heidi and ralf) as well as their average scores. So, in the class, I'm not sure how to use the static keyword and still print out the scores for each student:
//Object constructor / multiprototype method ():
function StudentMultiProto(name) {
StudentMultiProto.prototype.uniName = "XYZ";
this.name = name;
StudentMultiProto.prototype.avgScore = (scoreArray) =>
scoreArray.reduce((a,b) => a+b)/scoreArray.length;
StudentMultiProto.prototype.intro = () => {
console.log("My name is %s and I'm a student at %s.",
this.name, this.uniName)
};
};
let heidi = new StudentMultiProto("Heidi");
let ralf = new StudentMultiProto("Ralf");
console.log("The average score for each student: ");
console.log("Heidi: ", heidi.avgScore([64, 78, 59])); //returns: 67
console.log("Ralf: ", ralf.avgScore([85, 70, 67])); //returns: 74
// now, I would like to do the same but using class... what am I doing wrong?
class StudentClass {
static uniName = "XYZ";
constructor(name) {
this.name = name;
};
static avgScore = (scoreArray) =>
scoreArray.reduce((a,b) => a+b)/scoreArray.length;
static intro = () => {
console.log("My name is %s and I'm a student at %s.",
this.name, this.uniName);
};
};
let mark = new StudentClass("Mark");
let elen = new StudentClass("Elen");
//these return errors the "elen.avgScore is not a function"... how/where should I declare mark and elen to print their average scores?
console.log("The average score for each student: ");
console.log("Mark: ", mark.avgScore([64, 78, 59]))
console.log("Elen: ", elen.avgScore([85, 70, 67]));
// thank you for any help you can give me on how this works! I'm just starting now