23/30 Help me understand private properties in objexts


#1

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

Can you show me an example?

thanks!


#2

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:

https://www.codecademy.com/forum_questions/556d5b9fe39efe85980000a7

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.


#3

@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();
console.log(myBalanceMethod());

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


#4

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

console.log( john.bankBalance );

#5

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


#6

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
console.log(John.bankBalance);


#7

@letsgohyde,

1

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

2

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 )

3

// 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
console.log(John.bankBalance);


#8

@letsgohyde,

- - - 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")
OR

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' 
  };

OR
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
thus:

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

OR
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
  myObj.displayInstance();
  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.