23/30 Help me understand private properties in objexts


I get how private properties work, but how are they used in the real world?

Can you show me an example?



I am looking for an example in the archive, and hope to find what I'm looking for. If I recall, it was a score keeper program.

This thread surfaced that helps to explain this a little further:


Let's set up a scenario. You have a multiplayer game that keeps score, but we don't want players to be able to manipulate theirs or other members' scores. That is where private variables and methods come into play. Mull this over a bit, and see if you can come up with an example that we can work around. Meantime, I'll keep searching.


@mtf, this is 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
this.askTeller = function () {
return returnBalance();

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

And I m getting this error message : It looks like you didn't try to print john's bankBalance!
But it actually works...


After creating a new Instance
the code checker expects you,
to try and access the private property bankBalance

console.log( john.bankBalance );


weird... I thought I tried that first, but apparently I didn't. Thank you leonhard !


Hi #leonhard

Can you please help me understand what i'm doing wrong? I'm really lost here :frowning:

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","Potts", 35);

// try to print his bankBalance




variable-name's should be lowercase ( john ) , or camel-style ( johnIsMyname )


You have defined a Person object Class constructor
which has 3 properties
2 of them are defined with the this-keyword, which will make them _publicly accessable
1 is defined using the var keyword, causing the property to be privat to the Instance
and leading to the property NOT being publicly accessable (you will need a so-called =getter=-Method to get at the property-Value, which is done in the next lessons )


// create your Person
var john = new Person("John","Potts", 35);
// do a console.log() to see which =properties= are publicly accessable
console.log( john );
// try to print his bankBalance
// you will get an Error message as the =bankBalance=-property is NOT publicly accessable



- - - B a s i c s - - -

Each Object has one or more properties.
Each property consists of a property-key and it's associated value.

 var object1 = {
              name: "First"

So object1 has 1 property with property-key name
and it's associated value "FIRST" (which in this case is a "string-value")

var myObj = {
         type: 'fancy',
         disposition: 'sunny'

myObj has 2 properties seperated by a comma-,,
a property with property-key type and an associated-string-value 'fancy'
a disposition-property with property-key disposition and
..an associated-string-value 'sunny'.

To create an Object,
you can use the literal notation,
you directly create an Instance of the object, with the
properties being separated by a comma-,

  var myObj = {
        type: 'fancy',
        disposition: 'sunny' 

You create an Object by the construct notation.
First you create an empty Object by way of either
myObj = new Object; or myObj = {};
and then you attach its properties using the syntax
object-name.property-key = it's associated value

  var myObj = {};
      myObj.type = 'fancy';
      myObj.disposition = 'sunny';

There is also the facility Class construct notation.
The name should then start with a Capital-letter.

  var Person = function( theName, theAge ) {
         this.name = theName;
         this.age = theAge;
         this.displayInstance = function() {
           console.log("The displayInstance -output-"+
                       "\n============================" +
                     "\n\t name: " + this.name +
                     "\n\t age: " + this.age);
    //now create an Instance of this Class-object
  var myObj = new Person("Classy_Rocker",20);
    //call the Method displayInstance which takes NO parameters
  console.log( myObj );
  //you can run this in labs.codecademy.com try it !!

As you can see i created a function within this constructor,
they now call this function a Method.
So if in near future the course is asking you to create a method you now know
that you have to create
a property-key with an associated value being a function within an Object.