30. Private Eye


#1

Hi,

I just completed 30. Private Eye. I got code to work but I am curious why a version of my code wouldn't work before.

Here is the original code that wouldn't work:

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

and here is a version of the code that would work:

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

MY QUESTION: why can't we use "var this.getGPA = function() {" instead of "this.getGPA = function() {" ?
"


#2

It comes down to syntax.

this.getGPA = function()

What you're actually doing is adding a new method, getGPA, on the studentreport function. When declaring methods you don't use the var keyword.


#3

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