23. private variables


i added three arguments. but it did not work,
if i added two arguments.it showed undefined.
why ?

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,Bincent,23);

// try to print his bankBalance


23. Private Variables

I am not sure, but I think that your mistake is that you didn’t put ‘john’ and ‘Bincent’ between quotes.


function Person(first,last,age) { };
you created a so-called Object-Class-Constructor.
By using the new keyword you create a so-called instance of the Object-Class

In your
var john=new Person(john,Bincent,23);
the first and 2nd argument are seen by the Javascript interpreter as being
2 variables john and Bincent
You might want to change that in 2 strings like “john” and “Bincent”

After the Instance creation you might want to have a look at the object using a console.log()
console.log( john );
You will see the properties which are accessable by public
You used a var definition in your Class-Constructor leading to a
bankBalance property being private to the Instance
and will NOT displayed in your console.log() of the john-object !!!
console.log(john.bankBalance );
wil give an intended UNDEFINED error…
as John’s =bankBalance=. is PRIVATE, and NOT accessable by the PUBLIC


it is supposed to show undefined in this exercise.


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