25. Private Methods


#1


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);

john.askTeller returns the function and saves this in the variable myBalanceMethod. Now isn't the type of myBalanceMethod is a function. If yes the variable myBalanceMethod must be this :

myBalanceMethod = function() {
return bankBalance;
};

Now when we are calling this function how could it return the bankbalance as the variable bankBankbalance's scope is local and not global? I could not understand this. Help Please?