I don't understand why this variable was created? var myBalanceMethod


#1

I understand it holds the value of john.askTeller, I tried
this var myBalance = john.askTeller(); but that doesn't work.

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

#2

In your constructor you've created a private function and a public function, the function declared with var is private and scoped to be accessible only within the function. Then you created a function using this, which makes it available to objects, that runs the private function. However in your code, you didn't add the parentheses that tell the function execute like this:

this.askTeller = function() {
return returnBalance(); //<---- () added
};

Therefore when you access the method later and load into myBalanceMethod, JavaScript actually returns the function not the result. After you then run the function in myBalance, which returns the value.


#3

thanks for the help. I will tinker with that.


#4

That made so much since. Thank you!! :smiley:
Can you tell why I would ever want to make a method or variable private?


#5

Hi williab3

This sample will help explain in relation to JavaScript:

Code
function BankAccount(name) {
    this.name = name;
    var letter = function (){
        var randomAlpha = Math.ceil(Math.random()*26) + 65;
        return String.fromCharCode(randomAlpha);
    };
    var digit = function () {
        return Math.floor(Math.random()*10);
    };
    var baseID = letter()+letter()+digit()+digit();
    this.IDcode = name + baseID;
}

var customer1 = new BankAccount("Joe");
var customer2 = new BankAccount("Sam");
var customer3 = new BankAccount("Jan");

console.log(customer1.name, customer1.IDcode);
console.log(customer2.name, customer2.IDcode);
console.log(customer3.name, customer3.IDcode);
   function BankAccount(name) {
    this.name = name;
    var letter = function (){
        var randomAlpha = Math.ceil(Math.random()*26) + 65;
        return String.fromCharCode(randomAlpha);
    };
    var digit = function () {
        return Math.floor(Math.random()*10);
    };
    var baseID = letter()+letter()+digit()+digit();
    this.IDcode = name + baseID;
}

var customer1 = new BankAccount("Joe");
var customer2 = new BankAccount("Sam");
var customer3 = new BankAccount("Jan");

console.log(customer1.name, customer1.IDcode);
console.log(customer2.name, customer2.IDcode);
console.log(customer3.name, customer3.IDcode);

The function BankAccount is a constructor make new bank account objects. Inside it I want to make random ID codes, but this is needed only here in that constructor no where else. Using JS’s rules for private variables, I declare them with var, and the parts I want to be available I create with this.(whatever name). This means things are kept tidy, what is needed is encapsulated where it needs to be and can’t collide else where in a longer program. Variable names could end up similar or even reused. This opens up possibilities for bugs, security flaws and really lazy spaghetti coding littering the program (and browser) with global variables that get hard to manage. In real world web applications, your piece of JavaScript might be running on a webpage with other pieces of JavaScript, the chances then of name collisions gets high, and can create bugs that are really really hard to find.

In the code above though if you try:

console.log(customer1.baseID);

It will output ‘undefined’, it’s not needed outside the constructor so its kept private, as are its associated functions.

Take your time with the idea, it’s worth learning well. Good luck!