25/30


#1

I don't understand this at all. Can someone please show me a working code? Thank you!


#2

Here a custom (object / class) constructor is created:

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

The values of the first three properties/keys are being made public by using "this.".
The value of the fourth property/key is private because "var" is being used to create it inside the Person constructor, so it can not be accessed from outside of it.

var returnBalance = function() {
return bankBalance;
};

This creates a private method(aka function in an Object) - mind the "var" instead of "this." - to access the private value of it's private property/key "bankBalance". Since this method is made private aswell it will not be usable from outside, we need a method which is public within this Object that can run the private method/function:

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

By using "this." we make sure this method is public. Because it also is part of the Object which contains the private data we're trying to access it can help us by running the private function from within the Object itself.

The current bankBalance is static, though - means every new Person Object we create will have the same value of 7500 to it. This means the key bankBalance and the value 7500 was inherited.

The following code creates a new Person Object called john via the use of the custom constructor created above:

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

Here the private method is being called returning "undefined" in the console to illustrate that the value 7500 can not be accessed:

console.log(john.returnBalance);

This code creates two public variables outside of the Object in order to access the private data.
using console.log(john.askTeller) will return [Function]. As of my understanding we're following a chain here until we get the "key: value" we're aiming for:

var myBalanceMethod = john.askTeller();

console.log(myBalanceMethod) accesses the chain on a different "layer" but will still return [Function].

var myBalance = myBalanceMethod();

Finally this code will print out the right "key: value" since we "dug through" the layers or pulled to the end of the chain:

console.log(myBalance);


#3

Wow you are awesome #coderighton :smile:

Your explanation style should be used for all the answers in codecademy. Thanks for your useful and meaningful answer, I know understand exactly what this means.


#4

Omg thx! This worked!


#5

that is an awesome explanation! thanks.