24/30 Accessing Private Variables


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);

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

Can anybody tell me what im doing wrong? Thank you


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.


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


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