Private eye


#1

function StudentReport() {
this.grade1 = 4;
this.grade2 = 2;
this.grade3 = 1;
this.getGPA = function() {
return (this.grade1 + this.grade2 + this.grade3) / 3;
};
}

var myStudentReport = new StudentReport();

for(var x in myStudentReport) {
if(typeof myStudentReport[x] !== "function") {
console.log("Muahaha! " + myStudentReport[x]);
}
}

console.log("Your overall GPA is " + myStudentReport.getGPA());

Could anyone tell we haven't defined any parameters in
// var myStudentReport = new StudentReport();

how we are using it then??


#2

You're defining what would be the parameters as constants within the function. You could pass in g1, g2, g3 and set this.grade1 = g1, etc, called as var myStudentReport = new StudentReport(4, 2, 1); But instead you're defining them statically in the constructor itself.


#3

@recurvata thanks , now i got it!!


#4

I still need help!

Wait a minute...


#5

Something I don't understand: If we make grade1, grade1 and grade3 private variables, then how come we can still access them outside of the StudentReport constructor function? I know that when we construct the new myStudentReport object from the prototype StudentReport, then myStudentReport inherits all the properties of StudentReport. But how come we can access myStudentReport's grade1/grade2/grade3 variables for the purposes of adding/averaging them, but not for printing them?


#6

replace this with var and modify the getGPA by deleting the this.