Private methods (needs debug)


#1


https://www.codecademy.com/en/courses/objects-ii/4/4


function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
    this.askTeller = function(){
    };
}
   var returnBalance = function() {
      return bankBalance;
   };
       
   // create the new function here
   var askTeller = function() {
       return returnBalance;
};

var john = new Person('John','Smith',30);
console.log(john.returnBalance);
var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();
console.log(myBalance);

//The error is: TypeError: myBalanceMethod is not a function. 
//(In 'myBalanceMethod()', 'myBalanceMethod' is undefined)


#2

That private method is outside of the class body.

The above code can be removed. The code body (return statement) belongs inside your public method.


#3

What would be put instead?


#4

Here's the updated code:

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

}
// create the new function here
function askTeller(){
return Person.returnBalance;
}

var john = new Person('John','Smith',30);
console.log(john.returnBalance);
var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();
console.log(myBalance);


#5

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