25. Private Methods - parenthesis hurt brain

Wow! Knowing when to include the parenthesis, within the Class Property’s return statement, when assigning that function to a variable outside of the Class once an instance of the Object is created and then when requesting the value of the function spins my head. I got the results that the exercise wanted, but ouch.

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>


… return returnBalance;

…var myBalanceMethod = john.askTeller();

…var myBalance = myBalanceMethod();

<do not remove the three backticks above>
1 Like


++ 1/2 ++ 25/30 bankbalance explained
What they are trying to make clear,
is that after calling the public-accessable askTeller method

you still don’t have the actual value of the property-key -bankBalance

because the askTeller-function returns the name of a function

this function must be executed and then you get the Value of bankBalance.

A two-step approach, as you have to execute-twice.

As the () sign means execute, you could also get the result by using

console.log( john.askTeller()() );

++ 2/2 +++ 25/30 bankbalance explained
1 You have to create a so-called public askTeller Method
meaning you have to use the this-keyword and NOT the var-keyword

2 the askTeller Method should return returnBalance
and NOT as you are doing returning the bankBalance

= = = = = = = = = = = = = = = = = = = =

Here is the wording of the exercise…read it once more…

Private Methods

Why did that code in [24] work ?
An object’s private variables
can only be accessed by other methods
that are part of that same object.

So, in [24] we just used an object’s public method
to access a private variable!

Methods can also be private within a class and inaccessible outside of the class. Changing
from the last exercise to
var returnBalance
makes this method private.

If you run the program trying to access the method
you would get an undefined error this time.

The way to access a private method
is similar to accessing a private variable.

You must create a public method for the class, using the this-keyword
that returns the private method.


Create a method called askTeller within the Person class
that returns the returnBalance method.

This means that it returns the method itself
and NOT the result of calling that method.
So you should NOT have parentheses after returnBalance.

Because askTeller returns a method,
we need to call it to make it any use.
This is what var myBalance = myBalanceMethod(); does.


Your method should resemble how we defined getBalance last time—
you should use
this.askTeller = function() { }.

Don’t declare askTeller with var.

You return method the same way
that you would return simple variables.
Be careful not to call the method though
and leave out parentheses in your return statement!


Yes, this hurt my brain as well. When & why do we include parenthesis and when not to?

1 Like

We use parentheses whenever we wish to return the VALUE of any given method or function. In this case however, we want to return the entire METHOD, instead of the value that would be returned should we just run this method. The reason why, is because we need to refer to the method as a whole later on, and not just a value. Thus, we make a public method that accesses a private method, creating a storage space for the entire private method instead of just executing or running the private method. Does that make sense?


It makes perfect sense now…thank you very much for the explanation.

Truetoblue: Thank you for the explanation and in particularly for being so clear in your explanation. The explanation of the rationale behind the nomenclature and the neutral wording you used helped make your response sound helpful in tone too. Thanks again!

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