Question about private variables..(24/30)


So when we create a private varibale, can we add an instance for that private property? This is what I am asking..(consider the program from the link)..

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
   this.getBalance = function() {
      // your code should return the bankBalance
      return bankBalance;

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

// create a new variable myBalance that calls getBalance()
var myBalance=john.getBalance();

In the program above, we can only create john's instances for ('John','Smith',30) i.e. firstname, lastname and age but no bankBalance.(what i am trying to say is that we can't write var john=new Person(first,last,age,bankBalance)) so bankBalance has basically no uses?


This is a construction function designed to access objects.
and to access objects value, you have to call the
since var bankBalance is without property or value, you cannot access it as you would an object.
bankBalance is therefore not a constructor . The function is smart, looks at its constructs, and when doesn't find bankBalance, and realises that it doesn't fit in, it treats it as undefined.

bankBalnce is not useless there are times when you engaged in a large project, and you don't want to fall into the mistake of using an already existing variable, so you want to keep their scope private or local, to prevent them from being called upon into a global scope which might afftect or cause errors to your programme.

eg lets say you have 2 bank Balance
var bankBalance = 200;
var bankBalance = 300;
you will need to keep the sope of your banKBalances limited so you don't end with errors in your code. if one is within a private or local scope, it cannot be accessed by another in a global scope.