24. Accessing Private Variables - Question



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);

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


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;

=> [Function: square]

=> 81


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.



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:


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) {

// 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:


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