23.Private Variables - Question


Ok, so in this lesson, the instructions say "Private variables are pieces of information you do not want to publicly share, and they can only be directly accessed from within the class."

here is the code it gives you:

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

// create your Person 

// try to print his bankBalance

Very well, then it tells you to create a new object with the Person class constructor it gave you. But if you try to access the bankBalance with your new object, you'll notice you can't... My question is, WHY?

They say explicitly that it can only be accessed from within the class, and my new object HAS the "Person" class.
Maybe their explanation is a little flawed?



If you create an Instance and assign this Instance to the variable john

var john = new Person('John','Smith',30);**

and then do a


you will get an output =>

{ firstname: 'John', lastname: 'Smith', age: 30 }

If you have a close look at the output, it is clear
that you have created an Instance john
with 3 publicly accessible properties,
firstName property with property-key firstName and it's associated string VALUE "John"
lastName property with property-key lastName and it's associated string VALUE "Smith"
age property with property-key age and it's associated number VALUE 30

you will NOT see the bankBalance property
with a property-key bankBalance and it's associated number VALUE 7500

This is caused by the fact, that you were using the var-keyword,
in your template definition in the Person Class-constructor,
leading to this property being a private property to this object,
and NOT publicly visible as you have seen using the

You can NOT access this private bankBalance property directly via a Public-access

To access it you need to add a so-called getter Method,

HELP! Loop the Loop 27 - I'm lost!

Oh ok, i get it. So you will ALWAYS use a getter Method when you have a private property. So if, for example, i wanted to use a for loop in my object but exclude an specific property, i could make it private, but could still access it with a getter Method. Is that right?


Your assumption is correct....
Here a compilation of the course objectives....

An object has one or more properties, separated by a comma_,

A property has a property-key and it's associated VALUE

The VALUE can be anything from:

•Object  ◦Function

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;

++ 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!


Wow, thank you very much Leonhard!!!! This was really helpfull, i wish i could give you more likes hahahaha.


This is the best explanation i have ever seen here, i truly understand it,Hope all helpers will copy your style ND NOT RESORT to copy and paste answers that doesn't give further explanation and leaving you more confused. Thanks