25. Private Method, Parenthesis question


#1


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


EDIT: SOLVED.

I was always under the impression that when we call a method, we always use (). However, the code they give at the start doesn't seem to use the parenthesis:

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
}

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

console.log(john.returnBalance); //Why isn't this john.returnBalance() ?

Are the parenthesis optional when we are passing no parameter? The tutorial states that "If you run the program trying to access the method you get an undefined error this time." -- and it does show "undefined" when I run the program. However, when I try to add the parenthesises, the "undefined" message disappears. Why is that?

But I also notice that the john.askTeller() and myBalanceMethod() have parenthesis. Does this have something to do with it being private vs public?

Thanks in advance.

*EDIT: I didn't read the instructions closely enough. "This means that it returns the method itself and NOT the result of calling that method." I think I got it.


#2

meaning a string representation of the method is copied to the assigned variable. It can then be invoked later, outside of the class environment.


#3

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