24. Accessing Private Variables - Question


#1


https://www.codecademy.com/en/courses/objects-ii/4/3?curriculum_id=506324b3a7dffd00020bf661


My code works on this but most likely because Codecademy helped a lot in the instructions :wink:
How would I know to put empty parentheses after getBalance when calling it with
var myBalance = john.getBalance();


function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   this.getBalance = function() {
      // your code should return the bankBalance
      return bankBalance;
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()
var myBalance = john.getBalance();
console.log(myBalance);


#2

Method is just a function. In this exercise, you want to assign the result of the method to the variable. We are interested in the result of the function, so we have to execute the function.

To execute a function and optionally pass some parameters we use parentheses.


What happens when we omit the parentheses? We create an alias for the function:

var square = function(num) {
  return num * num;
};

x = square;

console.log(x);
=> [Function: square]

x(9);
=> 81

#3

Thanks, factoradic. I really appreciate the detail and it definitely helps.

I do have a question on your answer. You said if we don't use the parentheses we 'create an alias'. Does that mean you can omit the parentheses and just create an alias if that's how you want to do it? It looks like your square function worked as it was supposed to.

Kevin


#4

You're very welcome :slight_smile:

Well, you can omit the parentheses - that's not a syntax error. In the case of this exercise, you want to get the bank balance of your object. To get the balance you need to execute the method. So you have to make a function call => use parentheses.


Alias is just an alias. Please note that in the example provided by me I have created alias x to function square, but to calculate something I had to make a function x call (x(9)). So why not just use:

square(9);

I only wanted to show you what happens when you omit the parentheses. Now you can ask if it makes any sense to create an alias for a function.

Answer to this question is not so simple. Sometimes you want to temporarily mutate behaviour of the function that is already defined. Let's take a look at how codecademy evaluates the console.log statements. Normally when you use console.log you have to open the console (F12) to see the output. But in codecademy we can see the output in the visible HTML element, right?

To achieve this you can do something like this:

// create an alias for console.log
var copyOfConsoleLog = console.log;

// assign new value to console.log
// this is simplified version, now console.log will show the alert
console.log = function(message) {
  alert(message);
};

// test our new function,
// or execute the user code if we want to keep the codecademy example
console.log("Mutated behaviour");

// assign our original value to console.log
console.log = copyOfConsoleLog;

console.log("Normal behaviour");

I hope it makes some sense :slight_smile:


#5

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