Accessing Private Variables......Could it be this easy?


#1


https://www.codecademy.com/en/courses/objects-ii/4/3?curriculum_id=506324b3a7dffd00020bf661#


There was nothing for me to do in this exercise. The code automatically appeared correctly in the exercise. Did anyone else experience this stroke of luck?


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


#2

Are you sure you didn't already solve it another day?
Try resetting the code or refreshing the browser.
With me it appeared like it should.

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

#3

I'm not sure what happened. Thanks for the reply.


#4

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