23/30 I don't get it


#1

[quote="jaredb21, post:1, topic:4626, full:true"]
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
}

// create your Person
var john = new Person("john","lee",22);

// try to print his bankBalance
console.log(john.bankBalance);

I dont understand why we would put john.bankBalance.

if its a variable, then even if it wasn't private you still wouldnt automatically link the value and the object john to it through a '.'


#2

That is the author's way of showing us that we cannot access, john.bankBalance because it is private. We need a method that can return the balance which is covered in the next lesson(s).


#3

@mtf, this 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;
};

// create the new function here
this.askTeller = function () {
return returnBalance();
}
}

var john = new Person('John','Smith',30);
var myBalanceMethod = john.askTeller();
console.log(myBalanceMethod());

And I m getting this error message : It looks like you didn't try to print john's bankBalance!
But it actually works...


#4

Here we only want to return the string representation of the function, not the invoked return value.

    return returnBalance;

On its return from askTeller we will then invoke it. We need to understand what a closure is, which is way beyond this lesson, but that is what is at play here. Don't let this idea distract you, though. You will eventually learn about closures somewhere down the road.