24.Accessing private variables


#1


TypeError: john.askTeller is not a function


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


Replace this line with your code.


#2

You have an error here,

should be,


#3

I have copied wrongly into forum , i have given function Person(first,last,age)

i am getting following error:

Oops, try again. When checking john your code threw the following error: ReferenceError: john is not defined


#4

I did some adjustments and this is sure to work, but i'd like to see the link to your task just be sure that it's in line with the course requirement. Hope this was helpful

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

var returnBalance = function(){   
return bankBalance;
}//<---------------------------you didnt incapsulate your code well, the syntax was absent

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

};

var john = new Person('John','Smith',30);

console.log(john.returnBalance());//<---- not sure why you included this line of code since return balance is private

var myBalanceMethod = john.askTeller();//<--------problem here

var myBalance = myBalanceMethod;   //<-----balance method is not a function it's a varible

console.log(myBalance);

#5

Thank you this code worked


#6

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