23. Private Variables


#1

This is not working and i dont get why its not!!


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

// create your Person 
var john = new Person('Mou', 12);
var bankBalance();
console.log(john.bankBalance);
// try to print his bankBalance

Continuing the discussion from Private variables:


#2

Hi just remove that line

var bankBalance();

#3

yes i tried removing it but its showing undefined then!! why so?! its not printing what it should print!


#4

Another thing to fix is the fact that your constructor has three arguments, but you only use two when creating the Person object. John need both a first and last name.


#5

@mou_me yes, it's the right result because you are trying to eccess to a private variable


#6

still its showing undefined :unamused:


#7

ohh i see!!! thanks btw :open_mouth:


#8

you can't access the private variable.
The result is correct.


#9

check below code...

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

// create your Person

var john = new Person("Jogh", "p", 10);

// try to print his bankBalance

console.log(john.bankBalance);


#10

write

console.log( Person(bankBalance) );


#11

A post was split to a new topic: 23. private variables


#12