Why do we need to have an additional step to call for the method for private methods?


#1


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


I have solved the question but just a question about the need to call the method with var myBalance = myBalanceMethod(); to make it any use.

Why is it not working with the code as shown at the bottom below?

Full code (correct and working) :

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   var returnBalance = function() {
      return bankBalance;
   };
       
   // create the new function here
   this.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);

Why is it an error when i tried to change to this section

var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();

to just :

var myBalance = john.askTeller();

#2

For achieving this effect you could make askTeller call the returnBalance() function instead of returning function itself. Like this:

    this.askTeller = function() {
       return returnBalance();
    };

#3

Thanks for the explanation. I still don't quite get it, the method you outlined is already created in my "Person" function on top. So doesn't that means the public askTeller function will return the private method returnBalance when I use this ?

var myBalance = john.askTeller();

#4

When we start to do programming and encounter an issue, we will always have to debug. The first step to debug is to always find out any clue at all about what the issue is. Over here you mentioned you received an error. Knowing the context or what the error is about specifically will help you solve this problem.

Now, on to the question itself.

I'm going to assume when you say error here, what you get with the console.log is actually:

function () {
   return bankBalance;
}

This shows that john.askTeller() function is actually returning the variable returnBalance (which I am sure you know). However, that variable returnBalance is actually a function. A function has to be executed to process what ever it needs to. In this case, return returnBalance which is equals to 7500.

Thats why you need to do var myBalance = myBalanceMethod();
If you just want to get the value from var myBalance = john.askTeller();, you would have to change within the function askTeller to:

   // create the new function here
   this.askTeller = function() {
       // This is to execute the returnBalance function to get its value
       return returnBalance();
   };

My answer is specific to javascript. If you get error something like askTeller should return the method itself...., that is a codeacademy validation to ensure what you did follows their instructions :slight_smile:

*tip on how to better use console.log. Sometimes having too many console.log will confuse you as to which is which. What I normally do is to do it like this console.log('test: ',myVariableHere); <= test it out by adding the first parameter to your console.log and you will see the difference.


#5

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