24/30 Accessing Private Variables


#1

hey guys can you guys see what is wrong with my code ive had a look at others and they look exactly the same

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

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

#2

i keep getting this error

Oops, try again.
Your getBalance function should return bankBalance;.


#3

Hi this part

   this.getBalance = function() {
      // your code should return the bankBalance
      
   };

you should do what the comment aske you to do


#4

cheers, ive tried the code and i get this error now...

Oops, try again.
Your getBalance function should return bankBalance;.

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
      var bankBalance = john.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);

#5

this line

var myBalance = john.bankBalance;

where you put john.bankBalance it should be john.getBalance() the here


#6

still not working and i get the same error


#7

and here

   this.getBalance = function() {
      // your code should return the bankBalance
      var bankBalance = john.bankBalance;
      return bankBalance;
   };
}

remove this line

var bankBalance = john.bankBalance;

if it doesn't work an you post again your code


#8

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

#9

here

   this.getBalance = function() {
      // your code should return the bankBalance
      var bankBalance = john.bankBalance;
      return bankBalance;
   };
}

after you remove this line

var bankBalance = john.bankBalance;

the here

var myBalance = john.getBalance;

add the () to .getBalance


#10

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