Private Methods


#1

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

var returnBalance = function() {
return bankBalance;
};

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

It returns: It looks like myBalance doesn’t have the correct value! Make sure that askTeller returns the method itself and that you have not modified the provided code. I have not changed anything with the provided code. What am I doing wrong?


#2

Instance objects do not have a returnBalance attribute. It is a static (private) method accessible only to the class, itself. If this is in the instructions, it is to demonstrate that an error will be raised.

To show that the askTeller method returns another method, test it with,

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

#3

Got it. Thanks a lot!


#4

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