24/30 Accessing Private Variables


#1

Okay so, I am very confused! I am not sure why my code still isn't working..Here it is

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);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()
var myBalance = john.bankBalance
console.log(myBalance); 
john.getBalance();

Can anybody tell me what im doing wrong? Thank you


#2

How does this compare with what you wrote?

var myBalance = john.bankBalance

That should read,

var myBalance = john.getBalance();

The return value of this method will be the current balance.


#4

Ah,," ( )" that was what I needed. Why a pair of brackets is needed after "john.getBalance"?
Thanks!!


#5

Because `john.getBalance() is a method. By including the brackets (parens, parentheses) we invoke the function.