Accessing Private Variables


Given below is code in editor...

Accessing Private Variables exercise->Introduction to Objects II->JavaScript

Error message comes up over and over again as follows...
Oops, try again: you should print myBalance after setting myBalance to john.getBalance().

Please explain why this error message pops up time and time again...

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
   this.getBalance = function() {
      // your code should return the bankBalance
      return bankBalance;

var john = new Person('John','Smith',30);

// create a new variable myBalance that calls getBalance()
myBalance = john.getBalance();



Your last line


You can do two things. You can either use the variable myBalance and print that out using console.log since it already has performed the john.getBalance() function. Or you could add () to the end of john.myBalance (contained in console.log()) like you have previously so that it can be considered a syntactically correct call.

I would use the former since displaying a variable is less intensive than performing the function call again. But either is acceptable to advance to the next exercise.


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