Private variables


#1

What might be the problem with my code?? it prints 7,500 but i get this response,"Oops, try again.
Make sure to create john using the Person constructor"
Thanks in Advance
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
var myBalance=function(){
return bankBalance;
};
this.askTeller = function(){
return myBalance;
}
}

// create your Person
var John = new Person("john","ngash",37);
var myfirst = john.firstname;
var mylast = john.lastname;
var myage = john.age;
var myBank = john.askTeller();
var myBankBalance=myBank();

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


23. Private Variables
#2

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", "Smith", 30);

// try to print his bankBalance

var bankBalance = ()

//declare variable myAge set to the age of the john object.
console.log(john.bankBalance)

is what I got but the end bit isn't right


#3

i used ur formula first and got an error....i searched for the mothodology of handling private variables and got what i type above it prints the amount 7500 bt the code is not accepted..am really tossed here


#4

@ruddy12,
You will have to change the variable John
into john

var john = new  Person("john","ngash",37);

#5

i made changes but still there is an error though it still prints out 7500,sorry i have to use a screen shot ths tym,check it out


#6

@ruddy12,

right AFTER you created the john object
you should try a
console.log( john.bankBalance );
you will get an error, but that is expected....


#7

WOW!! Thanks it worked,phew!! i've been researching for weeks,cheers


#8

@ruddy12,

Next time, try to interpret the Oops- or Error message....


#9

@ruddy12,
endurance is good...

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
= http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
= http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1

Code-Year
https://www.codecademy.com/en/tracks/code-year?jump_to=4fce4211279bae0003006265

teach yourself javascript site:codecademy.com
https://www.codecademy.com/en/tracks/teachyourself
Meet JSON
https://www.codecademy.com/courses/javascript-beginner-en-xTAfX/0/1?curriculum_id=50802b7d5225bf0200000767
Adding Event Handlers The Simple Way
https://www.codecademy.com/courses/web-beginner-en-A0uwI/0/1?curriculum_id=50802b7d5225bf0200000767
JavaScript and Cookies
https://www.codecademy.com/courses/javascript-beginner-en-5knNv/0/1?curriculum_id=50802b7d5225bf0200000767
Dealing with the DOM
https://www.codecademy.com/courses/javascript-beginner-en-gwcYv/0/1?curriculum_id=50802b7d5225bf0200000767
JavaScript and CSS
https://www.codecademy.com/courses/web-beginner-en-SEgtM/0/1?curriculum_id=50802b7d5225bf0200000767

Yourself Python: http://www.codecademy.com/courses/python-beginner-en-fymF4