24/30 - why cant I use this


#1



24/30


Why can I not write this.bankBalance in the getBalance function instead of bankBalance? Is it because it is a local variable and not an attribute? Intuitively I thought this would work.


function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   this.getBalance = function() {
      return bankBalance;
      //return this.bankBalance;
   };
   this.getAge = function() {
    return this.age;
   };
   
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

var myBalance = john.getBalance(); 
var myAge = john.getAge();
console.log(myBalance);
console.log(myAge);


#2

Is it because it is a local variable and not an attribute?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty


#3

You are making things way too complicated for yourself in here. You only need to get the bankBalance not the age so you have extra lines of code in there. Also you never set john.getBalance(); so the code doesn't know where you got it for var myBalance. Also, you can not use this.bankBalance because it is a private attribute. It is set to where you have to call it by name instead of just adding it as a modifier.