25. Private methods. Just need an better explanation?;)


#1



Hi. Ok i did the task, but, can anyone explain me a little bit better? Line 19. and 20. to be precise, especially line 20. :wink:


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


#2

19. var myBalanceMethod = john.askTeller();

here you called the askTeller method that returns returnBalance method .

20. var myBalance = myBalanceMethod();

As myBalanceMethod holds the returnBalance method not the result of calling returnBalance method,you have to call it to make it any use.so here just called the returnBalance method.you can't get result without calling the method.that's what parentheses do here.

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

and here you don't have parentheses after returnBalance.because we dont want the result of calling the returnBalance method.with parentheses it'll give the result of' returnBalance' method.


#3

the .askTeller() method is a public method, and as such can be accessed by any instance of the Person class created(i.e John), hence we have john.askTeller().
Next we create a variable myBalanceMethod and equate it to john.askTeller(). This makes everything the john.askTeller() method holds to become accessible to the variable myBalanceMethod in line 20. We can go ahead to print it to the console as console.log(myBalanceMethod()); and get the same result, or go one step further and equate myBalanceMethod to another variable myBalance as we did in line 21. Which can then be printed to the console as console.log(myBalance);. Hope that explains it reasonably for you.:slight_smile:


#4

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