Private Eye


#1

https://www.codecademy.com/en/courses/objects-ii/5/4?curriculum_id=506324b3a7dffd00020bf661#

No error messages. Looking for an explanation with something in the code.

Can someone explain to me why we removed (this.) from the three grades and made them (var grade), but we're keeping (this.getGPA) instead of changing that to (var getGPA). I understand that changing (this.) to (var) makes that information public and not private, but I don't understand why (this.getGPA) is considered public? Thanks in advance for any explanation


function StudentReport() {
    var grade1 = 4;
    var grade2 = 2;
    var grade3 = 1;
    this.getGPA = function() {
        return (grade1 + grade2 + 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());


#2

this will make grade1, grade2 and grade3 private. grade1, grade2 and grade3 where public member variable, you changed them to private member variable.

anything that is added to this is public. So your method is public, your member variable are private.

This beautifully shows that we can access private member variable through a public method, but not access the private member variable directly


#3

Thank you very much! I had their concepts completely reversed. Now that makes perfect sense. Good thing for you moderators! Making simple mistakes like this, especially in the beginning can be mind boggling!


#4

i saw you understood it in reverse.

Good that now everything makes sense :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.