25: Private Methods


#1

Here's 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
var 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);

But it says that john.askTeller is not a function. And now I'm getting really confused because I pretty sure I did what the directions said


#2

I really suck at JS XD


#3

you should use
this.askTeller = function() {
return returnBalance;
};

and I think you miss one } at the end.


#4

Also adding on to @ajaxpro32712,

This the directions you are getting confused on:

Create a method called askTeller within the Person class that returns the returnBalance method.

This doesn't mean that you have to make a function in your way, since it has to be within the Person class. Do you remember what the function "this" does? In this situation, you have to make a function with this.askTeller that's in the Person class.

Like this:

this.askTeller ------------------

You know the rest. :smiley:


#6

thx everyone! It worked :smiley:


#7

What would I put for the this.askTeller function?


#8

@camclarke2000, @ajaxpro32712 has already said it. :smiley:


#9

Thing is, I don't know what to put as my function under this.askTeller Any help?


#10

Are there any other sites that teach you coding? I just dont think codecademy is right for me


#11

I want to learn to code games...not address books


#12

Hey @storm5638,

Yeah, that's actually my dream too. :smiley:

You can go to Unity, though hard (I actually never tried it), you can make games with that game engine there. :smiley:


#13

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

You were told not to declare a variable, but use:
this.askTeller = function() {
return returnBalance;
}